Casting error in my form
Posted
by
Siva
on Stack Overflow
See other posts from Stack Overflow
or by Siva
Published on 2011-02-19T06:18:01Z
Indexed on
2011/02/19
7:25 UTC
Read the original article
Hit count: 147
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)
{
}
}
© Stack Overflow or respective owner