0

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
                    }
Aaraadhana
  • 145
  • 1
  • 3
  • 14
  • Do you mean to select an already existing value in the ComboBox ? – V4Vendetta Jan 03 '12 at 07:49
  • yes....there are two ComboBox one will select the first one and the value of the next ComboBox will change its value according to the available items of the first ComboBox – Aaraadhana Jan 03 '12 at 07:53
  • If the corresponding value is available within the second combobox, all you would need to do is set the Cell.`Value` to the one you intend to be selected. – V4Vendetta Jan 03 '12 at 07:57
  • no suppose you selected some values in the first combobox then it should check for available fields from the database and show it on the second combobox...the concept is just like country and states combobox which you will find in most of the websites – Aaraadhana Jan 03 '12 at 08:01
  • Are you able to attach the eventhandler to your combobox. Please put a debugger and see if you get the correct combobox in cb. – Alok Jan 03 '12 at 08:43
  • Yes i can attach the eventhandler. But the problem is that i could not specify for which combobox the event should fire. The event is geting is fired for both the combobox and value is also changing for both of them. I want specific one for country and for state. Since i am creating it dynamically i can not use the name. I am using ComboBox cb = e.Control as ComboBox; but how to specify the event for one Combox and databind for the other one. – Aaraadhana Jan 03 '12 at 13:19

1 Answers1

0

Since you want to assign a different DataSource you can choose to add it to the second DataGridViewComboBox column itself or to that specific cell,

Since you would know the RowIndex and ColumnIndex you could very well set a different DataSource based on the first value selected

(dataGridView1[0,0] as DataGridViewComboBoxCell).DataSource = list of states

EDIT

Since you need to select the first item you can add this which should work fine

(studpromo_gv[14, semy] as DataGridViewComboBoxCell).Value = (studpromo_gv[14, semy] as DataGridViewComboBoxCell).Items[0]
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • DataGridView Combobox Value is not valid. Actually the code is fine. Values are binding but its not showing in the Combobox. Only when you select/click it then only its showing. The combobox should bind the first value by default rite. But its not happening. – Aaraadhana Jan 04 '12 at 07:00
  • I posted my Code with your implementation. you can check it out. – Aaraadhana Jan 04 '12 at 07:04
  • i guess if i put (dataGridView1[0,0] as DataGridViewComboBoxCell).SelectedIndex=1; It will show the first value. But SelectedIndex is not present for DataGridViewComboBoxCell – Aaraadhana Jan 04 '12 at 07:08
  • Actually you will have to assign the required value to say `dataGridView1[0,0].Value ="yourstate"`, [More on this here](http://stackoverflow.com/questions/5778621/set-selected-items-for-datagridview-combobox-failed/5778932#5778932) – V4Vendetta Jan 04 '12 at 07:10
  • But i don't know the value. User will be passing the value at Runtime – Aaraadhana Jan 04 '12 at 07:15
  • I posted the code you can check the ID and Name are dynamic. Upon selection the first value should appear in ComboBox – Aaraadhana Jan 04 '12 at 07:18
  • If its the first value you need in the combobox then `(row.Cells[1] as DataGridViewComboBoxCell).Value =(row.Cells[1] as DataGridViewComboBoxCell).Items[0]` – V4Vendetta Jan 04 '12 at 07:30