Casting error in my form
- by Siva
I have a ComboBox in a DataGridView. However I get an error when I run it:
Unable to cast object of type 'System.Windows.Forms.DataGridView' to type 'System.Windows.Forms.ComboBox'.
What can I do to resolve this error?
ComboBox comboBox;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
comboBox = e.Control as ComboBox;
if (dataGridView1.CurrentCell.ColumnIndex >= 0)
{
System.Diagnostics.Debug.WriteLine("Edit Control Showing");
comboBox.SelectedIndexChanged -= new EventHandler(comboBoxItems_SelectedIndexChanged);
comboBox.SelectedIndexChanged += new EventHandler(comboBoxItems_SelectedIndexChanged);
}
}
}
void comboBoxItems_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int comboBoxSelectedIndex = ((ComboBox)sender).SelectedIndex;
string comboboxSelectedValue = ((ComboBox)sender).SelectedText;
int gridViewSelectedRow = dataGridView1.CurrentRow.Index;
if (comboBoxSelectedIndex >= 0 && gridViewSelectedRow >= 0)
{
System.Diagnostics.Debug.WriteLine("ComboBox Index - " + comboBoxSelectedIndex);
System.Diagnostics.Debug.WriteLine("GridView Index - " + gridViewSelectedRow);
if (comboBox != null)
{
comboBox.SelectedIndexChanged -= new EventHandler(comboBoxItems_SelectedIndexChanged);
}
}
}
catch(Exception E)
{
}
}