combobox inside datagridview

rowlandsfc

Active member
Joined
Feb 3, 2019
Messages
43
Location
bridgend
Programming Experience
Beginner
i have a datagaridview that on form load loads details from a database into it, what id like to do if possible is add a combo box to one of the columns(status) andbe able to change this from either closed or open.

filling datagridview:
        private void showProducts()
        {
            try
            {
                conn = new SQLiteConnection(dbConnection.source);
                string sqlcommand = @"SELECT * FROM product";

                da_Products = new SQLiteDataAdapter(sqlcommand, conn);
                dt_Products = new DataTable();
                da_Products.Fill(dt_Products);
                dgv_Products.DataSource = dt_Products;

               

               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

got it working with this code:
private void dgv_Products_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.ColumnIndex > -1)
            {
                // Bind grid cell with combobox and than bind combobox with datasource. 
                DataGridViewComboBoxCell status = new DataGridViewComboBoxCell();

                // Check the column  cell, in which it click. 
                if (dgv_Products.Columns[e.ColumnIndex].Name.Contains("status"))
                {
                    // On click of datagridview cell, attched combobox with this click cell of datagridview 
                    dgv_Products[e.ColumnIndex, e.RowIndex] = status;
                    string[] state = new string[] { "open",  "closed" };
                    status.Items.AddRange(state);
                    


                }

            }
        }
 
Last edited:
Personally, if it's just a choice between two values/states, I feel like a checkbox is better than a combobox, but whatever floats your boat.
 
well i have added a checkbox to my datagridview but im trying to figure out how to set it so i can only check one box at a time

checkboxes added to datagridview:
// adds a checkbox to each row so user is able to select specific rows
            DataGridViewCheckBoxColumn dgvCmb = new DataGridViewCheckBoxColumn();
            dgvCmb.ValueType = typeof(bool);
            dgvCmb.Name = "Slt";
            dgvCmb.HeaderText = "Select";
            dgv_Products.Columns.Add(dgvCmb);
 
In your post #1, you were replacing the cell on the fly. In post #3 it looks like you have now opted to make the entire column a DataGridCheckBoxColumn.

Anyway, each row's checkbox should only map to changing the value for that row. Are you saying that when you change the value of one of the checkboxes on a row, all the other rows are also changing values?
 
No I'm not actually using the checkbook for anything atm what I want to use them for is to select one then on button click open a new form that will display all info that is relating to the selected row so ideally I'd only like to be able to select a maximum on 1 checkbox
 
If that's the case, why even use a checkbox column? Why not some kind of row click handler instead?
 
If you want to ensure that no more than one box is checked in a check box column then simply handle the CellContentClick event of the grid, which is raised when the user clicks a check box, and confirm that it occurred in the appropriate column, then set the Value to false in that column of every row but the one the event occurred in.
 
Back
Top Bottom