Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have datagridview on my form having 2 combobox.
One for Country and another for state.
I handle event for both combobox..
When i click on country combobox then it's selected change commited event fire and fill state. working properly...
But when i click on state then also country column event fire and reload the state column.
Please help me.......

private void dgvContactDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
switch (dgvContactDetail.CurrentCell.ColumnIndex)
{
case 1:
{
var cb = (ComboBox)e.Control;
cb.SelectionChangeCommitted -= new EventHandler(countryComboBox_SelectionChangeCommitted);
cb.SelectionChangeCommitted += new EventHandler(countryComboBox_SelectionChangeCommitted);
}
break;
case 2:
{
var cb = (ComboBox)e.Control;
cb.SelectionChangeCommitted -= new EventHandler(stateComboBox_SelectionChangeCommitted);
cb.SelectionChangeCommitted += new EventHandler(stateComboBox_SelectionChangeCommitted);
}
break;
default:
break;
}
}

private void countryComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
// Fill State Combo
}

private void stateComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
// Fill City
}
</pre>
Thanks,
Asif Huddani
http://asifhuddani.wordpress.com
Posted

I think if you look at the steps that occur, you will see what your problem was.

It's also important to understand that a GridView uses the same comboBox to do all of it's editing. So, let's keep track of the event handlers as you add or remove them. Let's call the comboBox for the GridView cboGridView

cboGridView Events

none


So, let's say that first, they edit the Country ComboBox. So the dgvContactDetail_EditingControlShowing method fires.

I assume that in this case, the dgvContactDetail.CurrentCell.ColumnIndex equals 1 if they're editing the Country information.

So, you remove an event handler for country changing (which for the first time run was never added). Then, you add an event handler for the country changing.

cboGridView Events

SelectionChangeCommitted: countryComboBox_SelectionChangeCommitted


So, now when they change the country, the states box fills in.

No, the user goes to edit the states box (which I assume is 2). So, what do you do?

You remove an event handler for stateComboBox_SelectionChangeCommitted which you hadn't added yet.

cboGridView Events

SelectionChangeCommitted: countryComboBox_SelectionChangeCommitted


Now, you add an event handler for stateComboBox_SelectionChangeCommitted.

cboGridView Events

SelectionChangeCommitted: countryComboBox_SelectionChangeCommitted &

stateComboBox_SelectionChangeCommitted


So, now, when the stateComboBox is changed, what will happen? Both SelectionChangeCommitted events will fire.

What you really want to do is remove the event handler at the end of both countryComboBox_SelectionChangeCommitted and stateComboBox_SelectionChangeCommitted like:
C#
private void countryComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
  // Fill State Combo

  //Remove handler
  (ComboBox)sender.SelectionChangeCommitted -= new EventHandler(stateComboBox_SelectionChangeCommitted);
}
 
Share this answer
 
I am doing this thing in windows...
 
Share this answer
 
Bind your county list under (!ispostback) event and enable viewstate true for data grid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900