I have a datagridview called GridView1 which has two column each are combobox's, I need to be able to change the items in one of the comboboxes based on the first one. The combobox are dynamically created and the values are bound with single dataset. Initially when i load the form every thing comes perfect but when i change the value of the first combobox the second combobox values do not change. I tried with EditingControlShowing event of the gridview and then applying SelectedIndexChanged to the combobox but still could not figure it out.
Grid view Combox
DataGridViewComboBoxColumn seccol = new DataGridViewComboBoxColumn();
seccol.DataSource = semch.Tables["secall"];
seccol.Name = "SSSS";
seccol.DisplayMember = "SSSSNAME";
seccol.ValueMember = "SSSSID";
seccol.HeaderText = "SSSS";
seccol.DataPropertyName = "SSSSID";
seccol.DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
studpromo_gv.Columns.Add(seccol);
studpromo_gv.Columns["SEC"].DisplayIndex = 14;
studpromo_gv.Columns[14].HeaderCell.Style.BackColor = Color.LightSeaGreen;
EditingControlShowing event
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
}
Updated Code
if (studpromo_gv.CurrentCell.ColumnIndex == 13)
{
ComboBox cmbBox = (ComboBox)sender;
//int semx = Convert.ToInt32(cmbBox.SelectedValue);
int semy = studpromo_gv.CurrentRow.Index;
if (cmbBox != null)
{
try
{
using (MySqlConnection conn = new MySqlConnection(MySQLconnection))
{
conn.Open();
MySqlDataAdapter secgvda = new MySqlDataAdapter("SELECT ID,NAME FROM STABLE WHERE SID='" + cmbBox.SelectedValue + "'", conn);
DataSet semch = new DataSet();
secgvda.Fill(semch, "secall");
(studpromo_gv[14, semy] as DataGridViewComboBoxCell).DataSource = semch.Tables["secall"];
(studpromo_gv[14, semy] as DataGridViewComboBoxCell).DisplayMember = "NAME";
(studpromo_gv[14, semy] as DataGridViewComboBoxCell).ValueMember = "ID";
conn.Close();
conn.Dispose();
}
}
catch (Exception)
{
//Some Statements
}