In a Windows forms application, how can I setup can I set up the SelectedIndexChanged handle for 4 d
Posted
by Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2010-05-17T02:12:43Z
Indexed on
2010/05/17
2:20 UTC
Read the original article
Hit count: 218
datagridview
|datagridviewcomboboxcolum
In a Windows forms application, within a DataGridView, I have 4 different DataGridCombobox controlshow can I set up the handler SelectedIndexChanged handler for the first combobox via the EditingControlShowing event. I added code for a second combobox but the SelectedIndexChanged didn't get wired up.
Here's my code. Any advice would be appreciated.
private ComboBox countryCombo;
private EventHandler countryHandler;
private ComboBox partCombo;
private EventHandler partHandler;
private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
countryCombo = e.Control as ComboBox;
if (countryCombo != null)
{
//remove any existing handler if there is one
countryCombo.SelectedIndexChanged -= countryHandler;
//add the new handler
countryCombo.SelectedIndexChanged += new EventHandler(countryCombo_SelectedIndexChanged);
}
if (partCombo != null)
{
partCombo.SelectedIndexChanged -= partHandler;
partCombo.SelectedIndexChanged += new EventHandler(partCombo_SelectedIndexChanged);
}
}
private void countryCombo_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox box = (ComboBox) sender;
//MessageBox.Show(box.Items.Count.ToString());
int rowNum = dataGridView2.CurrentCell.RowIndex;
dataGridView2.BeginEdit(false);
dataGridView2.Rows[0].Cells[2].Value = "abcdef";
dataGridView2.EndEdit();
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int cellColumn = e.ColumnIndex;
//MessageBox.Show("Column is: " + cellColumn.ToString());
}
private void partCombo_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox box = (ComboBox)sender;
string partNumber = box.SelectedValue as string;
// ToDo: now we need to get the HTSUS from the database so we can
//populate the field
int rowNum = dataGridView2.CurrentCell.RowIndex;
dataGridView2.BeginEdit(false);
dataGridView2.Rows[0].Cells[2].Value = "abcdef";
dataGridView2.EndEdit();
}
}
Al D.
© Stack Overflow or respective owner