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?
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.
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 = "1"; dt.Rows = "2"; dt.Rows = "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.Cells)).DataSource = lstStr;
Is this what you are looking for?
// Create a new Combo Box Column DataGridViewComboBoxColumn EmpIdColumn = new DataGridViewComboBoxColumn(); // Set the DataSource of EmpIdColumn as bellow EmpIdColumn.DataSource = myDataSet.Tables; // Set the ValueMember property as done bellow EmpIdColumn.ValueMember = myDataSet.Tables.Columns.ColumnName.ToString(); // Set the DisplayMember property as follow EmpIdColumn.DisplayMember = EmpIdColumn.ValueMember;