0

I have a DataGridView with a Column already defined as ComboBox. How do I add items to that Column Combobox so that they are available in that column for each new row?

I have tried everything I could think of or find on the web. Here's the latest attempt:

[![ static DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn(); static DataGridViewComboBoxCell dgvCmbCell = new DataGridViewComboBoxCell(); DataTable dt = new DataTable();

    private void frmReporting_Load(object sender, EventArgs e)
    {
        dt = Populate("SELECT DISTINCT OperatorName FROM tblOperator WHERE OperatorName NOT LIKE '*%' AND Active='1' ORDER BY OperatorName");
        dgvCmbCell = FillComboBox();
        dgvReports[1, 0] = dgvCmbCell;
    }

    private DataGridViewComboBoxCell FillComboBox()
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();

        //DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell();
        ArrayList row = new ArrayList();

        foreach (DataRow dr in dt.Rows)
        {
            row.Add(dr[0].ToString());
        }
        combo.Items.AddRange(row.ToArray());
        return combo;
    }

    private DataTable Populate(string sqlCommand)
    {
        myConnection.Open();

        SqlCommand command = new SqlCommand(sqlCommand, myConnection);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = command;

        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        adapter.Fill(table);

        myConnection.Close();
        return table;
    }

]1]1

Tim
  • 27
  • 3
  • There are many examples of how to add items to a `DataGridViewComboBoxColumn`. What have you tried? The combo box column has an `Items` property which you can manually add items to. It also has a `DataSource` property that you can assign a collection to. Post what you have tried if something doesn’t work. – JohnG Feb 24 '22 at 22:19
  • I tried everything I could find on the web. Here's the latest: – Tim Feb 24 '22 at 22:33
  • Well… is there some reason you are adding the items to a “single” `DataGridViewComboBoxCell` as opposed to adding the items to the `DataGridViewComboBoxColumn`? If the combo box cells have different items, then you may want to do this, however if all the combo box cells contain the same items, then set the items into the COLUMN not the CELLS. The column will set each combo box cell to the same items. – JohnG Feb 24 '22 at 23:02
  • Also, it appears you have a data source `dt` that may contain the items in the combo box cells. If this is the case then you could simply set the combo box columns `DataSource` to the `dt` variable. You should note that if you bind the “GRID” to a data source and one of the columns is the combo box data, then you will need to set the combo box columns `DataPropertyName` to point to the correct column in the grids data source. – JohnG Feb 24 '22 at 23:02
  • I would like to use the DataTable dt, but I don't know how to attach it to the DataGridViewComboBoxColumn. I tried: dgvReports[1, 0].DataGridView.DataSource = dt, but all I get is the number of rows in the DataTable containing empty ComboBoxes... – Tim Feb 25 '22 at 14:50

1 Answers1

0

Well… I am going to make a few assumptions here since you did not supply all the info needed. In the future you may get better results if you supply all the info that applies to your question as opposed to helpers having to ask these obvious questions.

First you show a picture of a grid that has a column named “Name.” This looks like a combo box column given the little down arrow. However, you do not show the code that “adds” that column to the grid. SO, I will assume two things here…

  1. the grid in the picture is named dgvReports
  2. that the combo box column shown in the picture was added by you in the “Designer” and that columns Name is “OperatorName.”

If this is the case, then you could change your current code to the code below to add the DataTable dt as a DataSource to the EXISTING column named “OperatorName” in the grid named dgvReports

private void frmReporting_Load(object sender, EventArgs e) {
  dt = Populate("SELECT DISTINCT OperatorName FROM tblOperator WHERE OperatorName NOT LIKE '*%' AND Active='1' ORDER BY OperatorName");
  DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dgvReports.Columns["OperatorName"];
  col.DisplayMember = "OperatorName";
  col.DataSource = dt;
}

Considering that the DataGridViewComboBoxColumn already exist in the dgvReports, then, it is unnecessary to “create” another combo box column or any combo box cells as your current code does.

If the Name of the combo box column in the grid is NOT named “OperatorName” then you need to change the code to the whatever Name the combo box column name has in the grid…

(DataGridViewComboBoxColumn)dgvReports.Columns["GridComboBoxColumnName"];
JohnG
  • 9,259
  • 2
  • 20
  • 29