andrewmanuja
Well-known member
- Joined
- May 30, 2019
- Messages
- 75
- Programming Experience
- Beginner
Hi,
I got a combo box and a datagridview and my requirement is, based on the user's selection of an item in the combo box (Category Name), the respective product details should populate in to the dataGrid View.
I got two tables in SQL Server named, tbl_Product (ProductID, ProductName and CategoryID) and tbl_Category (CategoryID, CategoryName).
My form load event looks like below;
The combo box selectedindexchange event looks like below;
Finally, I got a function named, "GetData" to call the sql command
Despite the application runs without errors, I only get the results related to the "CategoryID =1" , though I change the "CategoryName" value from the combo box, the dataGridView data does not change accordingly.
Appreciate a lot your valuable help to resolve this problem.
Kind regards,
Andrew
I got a combo box and a datagridview and my requirement is, based on the user's selection of an item in the combo box (Category Name), the respective product details should populate in to the dataGrid View.
I got two tables in SQL Server named, tbl_Product (ProductID, ProductName and CategoryID) and tbl_Category (CategoryID, CategoryName).
My form load event looks like below;
Form_Load event:
dataGridViewProducts.Rows.Clear();
dataGridViewProducts.Rows.Clear();
string query = "SELECT CategoryID, CategoryName FROM tbl_Category";
comboBoxCategory.DataSource = getData(query);
comboBoxCategory.DisplayMember = "CategoryName";
comboBoxCategory.ValueMember = "CategoryID";
comboBoxCategory_SelectedIndexChanged(null, null);
The combo box selectedindexchange event looks like below;
SelectedIndexChange event:
int val;
Int32.TryParse(comboBoxCategory.SelectedValue.ToString(), out val);
string query = "SELECT ProductID, ProductName,CategoryID FROM tbl_Product WHERE CategoryID ="+val ;
dataGridViewProducts.DataSource = getData(query);
Finally, I got a function named, "GetData" to call the sql command
Function:
cmd = new SqlCommand(query,con);
sda = new SqlDataAdapter(cmd);
dt = new DataTable();
sda.Fill(dt);
return dt;
Despite the application runs without errors, I only get the results related to the "CategoryID =1" , though I change the "CategoryName" value from the combo box, the dataGridView data does not change accordingly.
Appreciate a lot your valuable help to resolve this problem.
Kind regards,
Andrew