I have a datagrid with 2 combobox columns. I wrote selection changed event for the combobox column as follows.
private void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb!=null)
{ cb.SelectionChangeCommitted -= new EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectionChangeCommitted += new EventHandler(cb_SelectedIndexChanged);
}
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
var tb = datagrdADDTEMP.EditingControl as ComboBox;
if (tb != null)
str = tb.SelectedValue != null ? tb.SelectedValue.ToString() : null;
Assesment_Business_layer.Businesslayer bl = new Assesment_Business_layer.Businesslayer();
DataSet ds = new DataSet();**strong text**
ds = bl.GetSubCatNamesBA(str);
cmbDataGridSubCategory.DataSource = ds.Tables[0];
cmbDataGridSubCategory.DisplayMember = "SubCategoryName";
cmbDataGridSubCategory.ValueMember = "SubCategoryCode";
}
}
its working well with the first combobox column but the problem is the above selection changed event is also raising when i am selecting the item from the second combobox column..but i dont want to raise the selection changed event for the second combos column. It should raise only for the first combobox only.
Please help as l’m stuck up with this problem.
The problem seems that you’re adding the event handler to any combo box, doesn’t matter what column it is, so you must find first in what column the event was triggered, for this you must take a look at the sender object of the Grid_EditingControlShowing event handler (which is a DataGridView) and its CurrentCell, SelectedColumns or SelectedCells properties.
Example:
An example using SelectedColumns or SelectedCells, will be pretty much like this, if you want more info about that properties you can take a look at their documentation on MSDN