I have a DataGridView control that is bound to a DataTable, and I am using the DataTable to add rows to the DataGridView. I am not using a database in any way, but need to add data to the DataTable/DataGridView using code alone.

I need to replace certain columns with ComboBoxCells. The ComboBoxColumn setup won't work for me because each cell in the column needs combo boxes with different combo box values. I basically need to override each cell in that column as I am adding rows to the DataTable. My understanding is that I need to add rows to the DataTable, and then go in and specifically replace the cell in the DataGridView with the ComboBoxCell I created.

I have a method to create the ComboBoxCell object based on a list of strings I send it, but I can't seem to work out how to grab a specific cell, remove it, and replace it with the new ComboBoxCell. Can anybody point me in the right direction?

To isolate a certian cell you can use this:

DataTable dt = new DataTable();
            DataRow dr = new DataRow();
            

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (dt.Rows[i][j] == some condition)
                        dt.Rows[i][j] = something;

                }
            }

Where i is the row and j is the column. You can make this considerably more efficient by limiting the columns searched. Depending on the size of the datatable, this may be a large and time consuming search.

And you can set a cell to an object, however I am not sure what your ComboBoxCell is set up like, so it is difficult to tell how the datatable will react to it.

It would be helpful to supply your code for that, so we can look at it and test.

Hope that helps.

I believe I know how to isolate the exact cell, because I am using a drag & drop event which gives me a pointer to the current DataGridView (I am using multiple grids stored in a tab control), and using the current row property. At first I was trying to modify the combo box cell contents at the time I am setting up the grid.

I believe one solution to my problem would be to change the entire column to the combo box I need at run time, as the user is clicking. Basically, the user clicks on a combo box, and my program will do the lookup and modify it's contents before displaying it. If they want to change multiple combo boxes on that row, it would also work because I will go ahead and change all combo box columns when they click on one.

So my earlier problem is basically that I have the code to place a single combo box across all cells of a column, but how to place a different combo box in each cell of a column? Run-time might be the answer:

void grid_RowEnter(object sender, DataGridViewCellEventArgs e)
{
	DataGridView grid = (DataGridView)sender;
	DataGridViewComboBoxColumn cbc =  
                (DataGridViewComboBoxColumn)grid.Columns["Units"];
	cbc.Items.Clear();
	cbc.Items.Add("One");
	cbc.Items.Add("Two");
}

The above code would change all cells in the column with header "Units" into combo boxes containing choices One and Two. This happens as the user clicks the combo box. The real question here is whether it happens quickly enough, because the contents of the combo box comes from multiple lookup's in structures held in memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.