Question Adding data to a DataGridViewComboBoxColumn defined at design time

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
125
Programming Experience
10+
Hi,
I have a DataGridView where I have designed in the designer at design time. One of my columns is defined as a DataGridViewComboBoxColumn . At run time, I m trying to fill this ComboBox and not having much luck. Below is my latest attempt at doing so, but when I run the code, the ComboBox is still empty.

C#:
       private void frmReporting_Load(object sender, EventArgs e)
        {
            dt = Populate("SELECT DISTINCT OperatorName FROM tblOperator WHERE  Active='1' ORDER BY OperatorName");
            dgvCmbCell = (DataGridViewComboBoxCell)dgvReports[1, 0];
            FillComboBox();
        }

        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;
        }

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

            ArrayList row = new ArrayList();

            foreach (DataRow dr in dt.Rows)
            {
                row.Add(dr[0].ToString());
            }
            dgvCmbCell.Items.Clear();
            dgvCmbCell.DataSource = null;
            dgvCmbCell.Items.AddRange(row.ToArray());
        }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
125
Programming Experience
10+
I have only added items to the column, not to the cell. See the sample code in the documentation which adds the items to the column:
Looked at this one already. This one creates a new column on the end and doesn't address an existing column. Maybe there is no way to do it once you have created the DataGridViewComboBox in the designer. I guess I might have to break down and create the DataGridView by hand instead of using the designer...
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
The same way you found the cell and got a reference, you can also find the column a similar way and get a reference.

I suspect that the designer also allows you to get a variable declared that already references the column, but I'm not sure since I hand code my forms.
 

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
125
Programming Experience
10+
Skydiver,
I'm embarrased. Code was fine. I had the DataGridView set to ReadOnly. You can delete this thread. I tried, but I don't think I can.
 
Top Bottom