I seem to keep answering this question so I figured a CodeBank submission was warranted.
Stage 1.
1. Create a new WinForms application project.
2. Add a DataGridView to the form.
3. Add a BindingSource to the form.
4. Add the following code:Now run the project. Notice that the grid contains three TextBox columns. That is the default for most types of data. If you want a different column type for such data then you have to add the column yourself. Here's how to do that for a column of ComboBoxes.Code:Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As EventArgs) Handles MyBase.Load Me.BindingSource1.DataSource = Me.GetChildTable() Me.DataGridView1.DataSource = Me.BindingSource1 End Sub Private Function GetChildTable() As DataTable Dim table As New DataTable With table.Columns .Add("ChildID", GetType(Integer)) .Add("ParentID", GetType(Integer)) .Add("ChildName", GetType(String)) End With With table.Rows .Add(1, 3, "Child 1") .Add(2, 2, "Child 2") .Add(3, 1, "Child 3") End With Return table End Function
Stage 2.
1. Add a second BindingSource to the form.
2. Select the DataGridView and expand the smart tag (the little arrow at the top, right).
3. Click "Add Column".
4. Change the Type to DataGridViewComboBoxColumn.
5. Click "Add".
6. Click "Close".
7. Expand the smart tag again.
8. Click "Edit Columns".
9. Set the DataPropertyName of Column1 to "ParentID".
10. Click "OK".
11. Delete the previous code and add the following:Now run the project again. There are several things to note here:Code:Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As EventArgs) Handles MyBase.Load Me.BindingSource2.DataSource = Me.GetParentTable() Me.Column1.DisplayMember = "ParentName" Me.Column1.ValueMember = "ParentID" Me.Column1.DataSource = Me.BindingSource2 Me.BindingSource1.DataSource = Me.GetChildTable() Me.DataGridView1.DataSource = Me.BindingSource1 End Sub Private Function GetParentTable() As DataTable Dim table As New DataTable With table.Columns .Add("ParentID", GetType(Integer)) .Add("ParentName", GetType(String)) End With With table.Rows .Add(1, "Parent 1") .Add(2, "Parent 2") .Add(3, "Parent 3") End With Return table End Function Private Function GetChildTable() As DataTable Dim table As New DataTable With table.Columns .Add("ChildID", GetType(Integer)) .Add("ParentID", GetType(Integer)) .Add("ChildName", GetType(String)) End With With table.Rows .Add(1, 3, "Child 1") .Add(2, 2, "Child 2") .Add(3, 1, "Child 3") End With Return table End Function
1. The grid still only contains three columns but one of them contains ComboBoxes.
That's because, by setting the DataPropertyName of the column, you told it to bind to that column of the grid's DataSource. As such a new column was not added to the grid for that column of the DataSource.
2. The columns are not in the same order as they were before.
You can force the columns to be displayed in the order you want by adding them all at design time and not relying on the grid to auto-generate any of them. You can also mess with the DisplayIndex properties of each column but that is something for you to play with.
3. The ParentID column now shows the ParentName values from the parent table, rather than the ParentID values from the child table.
This is a function of having set the DisplayMember of the column, which determines which column from it's DataSource gets displayed.




Reply With Quote