GridView not updating when data comes from multiple tables - asp.net

I have a GridView control that gets data from two tables, the first one contains a primary key, a name (string) and a foreign key to the second table, The second table contains a primary key "referenced by the foreign key mentioned" and a name (string), i was able to display the id, name (first table) and the name (second table) using an inner join but i can't update the data in the tables using the GridView (when pressing update nothing happens at all, or no change occurs).

From what i understand of your question what you have is a situation that you must a apply a nested gridview.
A gridview shows the content of a table (datatable or a collection).
If you want to show other collection that is inside each row of your primary gridview, you will need
to build a second Gridview or listview or repeater to show that information.
with the primary griview you can use the OnItemDataBound to assign the datasource of the nested gridview or what you choose to show that information based in primary key of the row.

Don't bother i found a solution to the problem on Microsoft website, i am sorry i didn't make my question pretty clear and here is a link to what i was looking for: Editing with Template Fields

Maybe after clicking Update you need to call your read method again to refresh your gridview.
something Like:
UpdateMethod()
{
//YOUR UPDATE STUFF
//REBIND DATA WITH UPDATED RECORDS
RefreshMethod(); //YOUR BINDING METHOD TO DATAGRID STUFF
}

Related

How to show all items in Listbox that is linked to dropdown

In an Access Userform I have a listbox that is linked to a dropdown field. In the dropdown field the user can choose a country, and the listbox then updates and shows only the records that are relevant to that country.
I have described how I get the listbox to filter based on the selection in the dropdown in this question here.
While I managed to solve one problem there I created another.
Now when I open the form, the listbox is empty instead of showing all records.
My question: How can I show all records in the listbox to begin with and THEN have the user filter the list based on the dropdown?
The SQL Code in the LIstbox is now the following:
SELECT tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR, tblISIN_Country_Table.Country
FROM tblFUNDS INNER JOIN tblISIN_Country_Table ON tblFUNDS.ISIN = tblISIN_Country_Table.ISIN
GROUP BY tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR, tblISIN_Country_Table.Country, tblFUNDS.Fund_Selection
HAVING (((tblISIN_Country_Table.Country)=[Forms]![frmMain]![ddnCountry].[Text]) AND ((tblFUNDS.Fund_Selection)=0));
So I ended up solving this one as well... with a bit of help from an online article which I can now no longer find unfortunately (otherwise I would reference it here):
SELECT tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR, tblISIN_Country_Table.Country
FROM tblFUNDS INNER JOIN tblISIN_Country_Table ON tblFUNDS.ISIN = tblISIN_Country_Table.ISIN
GROUP BY tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR, tblISIN_Country_Table.Country, tblFUNDS.Fund_Selection
HAVING (((tblISIN_Country_Table.Country) Like Nz([Forms]![frmMain]![ddnCountry].[Text],'*')) AND ((tblFUNDS.Fund_Selection)=0));
The important part is this...
Like Nz([Forms]![frmMain]![ddnCountry].[Text],'*')) AND ((tblFUNDS.Fund_Selection)=0));
Essentially the Nz function lets you return a value when a variant is null. I had it return * which ofcourse is the SQL equivalent of Return All.

Gridview: column with different controls is different modes

Here is my scenario.
I have an objectdatasource-bound gridview where i want to show Students {name, age, class}
name:string, age:int, Class:Foreign key, int
I also want to edit it, but when doing so i want to show a dropdownlist for the class field.
Currently i solve this by
a)creating a view, binding the grid to that view
b) on the editItemTemplate, put a DDL and bind it to the Class table.
What i want to know is if there is a way without involving the view shortcut.
NOTE: this question, Bind column to a different entity source than the gridview is
is somewhat related, but not quite the same thing. in that example, the OP just wanted to show the related data. Since am also updating, i need both the key and the value.

In ASP.NET, Can I Delete a row in GridView while the primary key of that table is not loaded into the gridview?

I have a table with primary key whose importance is technically high and none business wise.
So, when I display the table data to user, I would like not to show him the primary key.
Even if I load it in sqldatasource and dont show (I did this by removing the cloumn in "Columns" tag of GridView), I am not able to update or delete row using the built-in UpdateCommand & DeleteCommand.
I have created the delete paramters but it does nothing when I hit the delete button.
How to get around it?
Thanks
R S Reddy
Use the primary key as the CommandArgument for the delete button. This say, you still have server side access to the value you need without having to expose it on the front end.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
Well, I got this fixed by using DataKeyNames attribute in GridView.
Just added primarykey of my table into it and everythings working like charm.
Thanks for your prompt help Babak.

how to add data into a specific column at runtime in grid

I have a rad grid, i have bounded the columns in the grid using GripBoundColumns which shows me dropdown cloumns when i edit the record, Insert/update/delete are working fine for me.
My question is it possible to insert new data into the column(Not the whole record just only to one column) when i edit the record.
For example...
suppose i have 5 columns (Client Name, Account No, Account name, account status, Custodian Dealer)
CustodianDealer is my drop down coloumn and data for it comes from different table
when i edit or insert a new record in the grid, i can select the existing Custodiandealers in the table.
now if i want to insert a new record to the custodianDealer table not to the grid, how can i acheive it
Try something like this within the ItemDataBound event:
If TypeOf (e.Item) Is GridDataItem Then
Dim GDI As GridDataItem = CType(e.Item, GridDataItem)
GDI([ColumnNameAsString]).Text = "Some custom text"
End If
The GridDataItem is essentially a TableCell, so you can add controls as needed.
Oh, and if it wasn't clear from my notation, "[ColumnNameAsString]" is your own column name.
Wire the grid insert command and execute an insert query which updates merely the Custodiandealers table with a new entry. To display it in the dropdown editor, make sure the new record has a foreign relation with the grid main table source as the rest of the items in the Custodiandealers table.
So, if I'm reading your problem correctly, you are trying to insert a record into the custodianDealer table based on some changes made during edit/insert.
If that's the case, you can handle the ItemCommand event and update your datatable based on those changes.
Can you provide the exact details of the use case?

Coding practice: how to avoid hard coding?

I have a table in the database that store 4 category and the structure of the table is ID (GUID), description. I load the category into a dropdown list (asp.net webform) to allow people to select a category and based on what they selected. I'll then display info associated with their selection and hide the others.
Currently, i do a "select case" based on the GUID that i hard coded in code behind to display the associated info. Is there a better way to do this without hard code in GUID on the code behind?
What is the Data that's associated with the Guid/Description...
The data you've hardcoded sound's like a candidate for being added to the database itself.
If it's one piece of information per Category/Guid, then consider extending your Database Table to store that info to.
If it's multiple piece of information per Category/Guid, then consider creating a new Table With a CategoryID on it, and a foreign key relationship between your Category Table and your ExtraInfo table
You could query the database for the GUIDs when the app starts and cache them in a static Dictionary.
you could store the GUID in your web-config and load it at run time. then, you can easily replace that GUID with another w/o having to recompile.
You should have a Categories table and a Posts table (or whatever it is that you will tag with your categories). In the Posts table you have a column for the CategoryID (assuming each post can only belong to one category), so that you only have the category name in one place (normalize your data).
When you render the dropdownlist, you select the GUID:s from the database. No hard coding, and if you add another category (or remove one) the dropdownlist will automatically reflect the available categories.
If you bind the dropdown list to the Category row or a Tuple that contains the category name and the value you can load the Guid in your codebehind using the SelectedValue property. You will then set the DataTextField and DataValueField on the Dropdownlist.

Resources