0

I want to add Combobox to the second column in Data Grid View which already has in SQL table data. SQL table value wants to be selected item of combobox and wants to additional option for choose.

I want to add combobox to the data type column. SQL table can have the only one which can be Int but wants to add options to select like float, string, etc.

The following code will add a new column not add to the existing SQL table column. How to add combobox to an existing column of SQL table shown in datagridview.

All data show in Data Grid View.

                connection = new SqlConnection(connetionString);
                sql = "select * from Devicereg";
                try
                {
                    connection.Open();
                    adapter.SelectCommand = new SqlCommand(sql, connection);
                    adapter.Fill(ds);
                    connection.Close();
                    dataGridView1.DataSource = null;
                    dataGridView1.ColumnCount = 0;
                    dataGridView1.DataSource = ds.Tables[0];
                    dataGridView1.Columns[0].HeaderText = "ID";
                    dataGridView1.Columns[1].HeaderText = "Parameters";
                    dataGridView1.Columns[2].HeaderText = "Addresses";
                    dataGridView1.Columns[3].HeaderText = "Data Types";

                    dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

                    DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
                    cmb.HeaderText = "Select Data";
                    cmb.Name = "cmb";
                    cmb.MaxDropDownItems = 4;
                    cmb.Items.Add("Floart");
                    cmb.Items.Add("Int");
                    dataGridView1.Columns.Add(cmb);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                } 
saji indra
  • 57
  • 1
  • 1
  • 9

1 Answers1

0

I Guess, you can try below code. I hope this will help.

DataGridViewComboBoxColumn cmbCol = new DataGridViewComboBoxColumn();
                cmbCol.HeaderText = "yourColumn";
                cmbCol.Name = "myComboColumn";
                cmbCol.Items.Add("True");
                cmbCol.DataSource = myList();
                dataView.Columns.Add(cmbCol);
                dataView.Columns["myComboColumn"].DisplayIndex = "at any index that you want";

                foreach (DataGridViewRow row in dataView.Rows)
                {
                    row.Cells["myComboColumn"].Value = row.Cells["yourColumn"].Value;
                }

Ref : https://stackoverflow.com/a/34013997/10945191