Binding a DataGridView to a DataTable but also including a ComboBox as the last column

Right now I have a DataGridView that's bound to a DataTable that works all well and good. What I need to do on the DataGridView is include a new column at the very end that is a ComboBox that will dynamically filly based on the key value of the row.

My columns are ID, Name and Count. The 4th column will be a ComboBox that takes an ID and creates the values in the drop down based on that. I currently have a custom ComboBox that takes an ID in its constructor and fills it that way, but I couldn't figure out a way to put that into the DataGridView. So, I created a copy of that custom ComboBox control and made it into a DataGridViewComboBoxCell but I STILL can't figure out how to dynamically bind it to the form. I've scoured the internet and found some examples but not exactly what I want to do.

This link shows kind of what I want to do except I'm using C# not VB. Any ideas?

13.10.2009 15:26:52
3 ОТВЕТА

You can take or leave this way, but the way I've handled it (in VB) is to bind the DGV to a collection of objects which contains some extra members above and beyond my actual bound data. I use these additional members as placeholders for extra stuff.

I populate the elements from the data source (a DB in my case) into the collection, populate the other members as I need them, and then bind the collection to the DGV. Since the extra members are not members of the original table, I can change their contents. Then I don't really have to deal with "partial binding" of the control as you want to do.

Anyway, another way to skin the cat.

0
13.10.2009 15:36:00
I was thinking of doing this but at the same time wanted to keep this light-weight by not having to create a brand new custom class... I'll keep this in mind though.
pikes 13.10.2009 15:48:22

Here is what I just tried:

DataTable dt = new DataTable();
            dt.Columns.Add("Col");
            dt.Rows.Add();
            dt.Rows.Add();
            dt.Rows.Add();
            dt.Rows[0][0] = "1";
            dt.Rows[1][0] = "2";
            dt.Rows[2][0] = "3";
            dataGridView1.DataSource = dt;

            dataGridView1.Columns.Add(new DataGridViewComboBoxColumn());

            List<string> lstStr = new List<string>();
            lstStr.Add("1");
            lstStr.Add("2");
            lstStr.Add("3");
            lstStr.Add("4");

            ((DataGridViewComboBoxCell)(dataGridView1.Rows[0].Cells[1])).DataSource = lstStr;

Is this what you are looking for?

3
13.10.2009 15:51:41
Not exactly, what I need to do is dynamically build the dropdown based off of whatever's in column 1 of the data table. I don't want to have to make 2 passes on the data if I don't have to - I would rather get the ComboBox list when the row is added. I don't know if there's an event I can do that in, though.
pikes 13.10.2009 16:12:37
There is a UserAddedRow and RowAdded event for the datagridview. Probably you can make use of that.
danish 13.10.2009 16:18:46
If I'm populating from a datasource, RowsAdded does all rows at once. Looks like I might have to make 1 pass to load the data, and one pass to populate the dropdowns...
pikes 13.10.2009 16:31:41

Try this:

// Create a new Combo Box Column

DataGridViewComboBoxColumn EmpIdColumn = new DataGridViewComboBoxColumn();

// Set the DataSource of EmpIdColumn as bellow

EmpIdColumn.DataSource = myDataSet.Tables[0];

// Set the ValueMember property as done bellow 

EmpIdColumn.ValueMember = myDataSet.Tables[0].Columns[0].ColumnName.ToString();

// Set the DisplayMember property as follow

EmpIdColumn.DisplayMember = EmpIdColumn.ValueMember;
2
17.11.2009 23:49:05
I'm knee deep in other stuff right now but when I get some time I'll go back and see if this works the way I was thinking it should. Thanks!
pikes 19.11.2009 15:53:27