By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,686 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,686 IT Pros & Developers. It's quick & easy.

Display records on the grid based on selected item in the combobox

P: 26
i created a form that should function as follows:

i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a different item no in it..then, these records will be saved in a temporary datatable (which i made in a separate class, the name is WBASKET)...The item nos of these records will be displayed in a combobox, say item1, item2, etc..

then,i have a datagrid in the form..what will be displayed in it are those records saved in the Temporary datatable that i made, not directly querying from the database.

i already created a function that will let the program display on the grid the records that have the item no that i selected in the combobox...say i selected item1, then all records which has item1 in the temporary table will
be displayed on the grid.

//function for displaying records on the grid which has the same item no as the one selected in the combox
//wbasket is the temporary datatable that i made.
//Name: FillGridByItem()

DataTable dt = new DataTable();
dt = wbasket.DataTable.Clone();
int index = 0;
foreach(DataRow dr in wbasket.DataTable.Rows)
if(dr["item_no"].ToString().Trim() == cmbItems.SelectedItem.ToString().Trim())
DataRow dr2 = dt.NewRow();
int i = 0;
foreach(DataColumn dc in wbasket.DataTable.Columns)
dr2[i] = dr[dc.ColumnName];
dgrWarehouse.DataSource = null;
dgrWarehouse.DataSource = dt;

after displaying all those records in the datagrid, once i click a record, then these data will be displayed in their corresponding textboxes...say i have a textbox for item name..then the item name field from my selected index in the grid will be displayed...(still the selected item in the combobox is the same as the item no that i clicked on the grid..)..up to there it is working fine.

the problem is this..say i have two items in the combobox. i selected the second item, example item2..then the program will display all records with will display the data to their corresponding textboxes, but when i click
the save button to update the temporary datable that i made,what is being updated is the first index, which is item1, not item2..

//UpdateGrid() function --being called when i click the save button

DataTable dt = (DataTable)(dgr1.DataSource);
if(dt.Rows.Count > 0)
if (dgrWarehouse.IsCellSelected)
DataRow dr = wbasket.DataTable.Rows[dgr1.CurrentRowIndex];
dr["quantity"]= txtQuantity.Text;
dr["name"] = txtName.Text

i hope you can help me with this...thanks..
Nov 13 '08 #1
Share this Question
Share on Google+
2 Replies

P: 26
i know the error occurs in DataRow dr = wbasket.DataTable.Rows[dgrWarehouse.CurrentRowIndex]; under the UpdateGrid function, because the currentRowIndex of the grid is different from the wbasket's index, since i fill the grid by item no (FillGridByItem function)..

say i have displayed one item in the grid, which is item2..since i filtered it first based on the selected item in the combobox, item2 becomes the first index in the grid..but in the temporary datatable, the row number of item2 is 2...

but i don't know how to correct it.. :(
Nov 14 '08 #2

P: 26
got works fine now...:)
Nov 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.