I placed combobox as one of the cells in datagridview.when i have to update a row i m selecting a value from the combobox but i m able to select the value from the combobox not after a single but after 2 or 3 clicks
why is it happening i m not able to know.And when i m trying to get the selected value of combobox and the selected Item i m getting only from the first row combobox.When i am trying to select the 2nd row combobox i am getting -1 as the selected index after 3 clicks on combobox and nothing as selectedItem.
Any help please
public partial class Form1 : Form
{
SqlConnection sqlcon = new SqlConnection("Data Source=RAJANI\\SQL2005;Initial Catalog=usha;Integrated Security=True");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
fillgrid();
//code for populating the combobox
DataSet ds1 = new DataSet();
SqlDataAdapter da1 = new SqlDataAdapter("select * from states", sqlcon);
da1.Fill(ds1,
"states");
colstate.DataSource = ds1.Tables[
"states"];
colstate.DisplayMember =
"state";
colstate.ValueMember =
"number";
colstate.HeaderText =
"STATES";
colstate.AutoSizeMode =
DataGridViewAutoSizeColumnMode.ColumnHeader;
}
public void fillgrid()
{
//code for filling the datagridview
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from citys", sqlcon);
da.Fill(ds,
"citys");
dataGridView2.DataSource = ds.Tables[
"citys"];
}
private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox editingComboBox = (ComboBox)e.Control;
if (editingComboBox != null)
editingComboBox.SelectedIndexChanged +=
new System.EventHandler(this.editingComboBox_SelectedIndexChanged);
}
private void editingComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox colstate= (ComboBox)sender;
//Display index
MessageBox.Show(colstate.SelectedIndex.ToString());
//Display value
MessageBox.Show(colstate.Text);
}