SelectedValueChangedEvents firing at the wrong time.
Hi ,
I am working on a program that uses a DataGridView with 12 columns. 2 of these columns are DataGridViewComboBoxColumns and the other 10 DataGridViewComboBoxColumns. When you change the value in either of the ComboBoxColumns the values in the rest of the columns for that row are supposed to update themselves. I have created two separate events to handle the SelectedValueChanged event for the two ComboBoxColumns and somehow they are overlapping/firing at the wrong time.
Here is the event that adds the event depending on which column gets clicked:
Code:
private void DataEditingControlShowingEvent(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView grid = sender as DataGridView;
if (grid != null)
{
DataGridViewComboBoxEditingControl cbx = e.Control as DataGridViewComboBoxEditingControl;
if (cbx != null)
{
if (grid.CurrentCell.ColumnIndex == grid.Columns["Pattern"].Index)
{
cbx.SelectedValueChanged -= this.PatternBoxSelectedValueChangedEvent;
cbx.SelectedValueChanged += this.PatternBoxSelectedValueChangedEvent;
}
if (grid.CurrentCell.ColumnIndex == grid.Columns["ID"].Index)
{
cbx.SelectedValueChanged -= this.IdBoxSelectedValueChangedEvent;
cbx.SelectedValueChanged += this.IdBoxSelectedValueChangedEvent;
}
}
}
}
Here is the event for if someone clicks the ID column:
Code:
private void IdBoxSelectedValueChangedEvent(object sender, EventArgs e)
{
// this event causes dynamic change for the calculated values when the ID
// drop down changes selection.
ComboBox cbx = sender as ComboBox;
if (cbx != null && cbx.SelectedIndex > -1)
{
int ballqty;
int id = (int)cbx.SelectedValue;
PatternXML pat = GetPatternById(id);
if ((int.TryParse(datagrid.CurrentRow.Cells["BallQty"].Value.ToString(), out ballqty)) &&
pat != null && datagrid.CurrentRow.Cells["Pattern"].Value != pat.Value)
{
//if the pattern doesnt match already force it to
datagrid.CurrentRow.Cells["Pattern"].Value = pat.Value;
datagrid.CurrentRow.Cells["Frequency"].Value = PaytableCalculator.CalcFrequency(pat, ballqty);
}
}
// control.SelectedValueChanged -= IdBoxSelectedValueChangedEvent;
}
Here is the event that is supposed to fire if the Pattern column is selected:
Code:
private void PatternBoxSelectedValueChangedEvent(object sender, EventArgs e)
{
// this event causes dynamic change for the calculated values when the Pattern
// drop down changes selection.
ComboBox cbx = sender as ComboBox;
if ((cbx != null) && (cbx.SelectedIndex > -1))
{
int ballqty;
PatternXML pat = cbx.SelectedItem as PatternXML;
if ((int.TryParse(datagrid.CurrentRow.Cells["BallQty"].Value.ToString(), out ballqty)) &&
pat != null && datagrid.CurrentRow.Cells["ID"].Value.ToString() != pat.ID.ToString())
{
datagrid.CurrentRow.Cells["ID"].Value = pat.ID;
datagrid.CurrentRow.Cells["Frequency"].Value = PaytableCalculator.CalcFrequency(pat, ballqty);
}
}
// control.SelectedValueChanged -= PatternBoxSelectedValueChangedEvent;
}
Any ideas you guys could give would be awesome.
PS - also posted at http://stackoverflow.com/questions/6...ew-event-issue
Thanks.
Hunter