0

This MSDN article suggests it is possible to change the content for an auto generated data grid view with your own data and re-bind one of its columns, changing the type. It does not describe exactly how to proceed though. There are a number of questions similar to this one, but most of them essentially point towards MSDN sources and have no real pointers how to exactly proceed. Most importantly it's fairly unclear what properties to set in your custom column to make sure it works the same way an automatically generated column would. This question details a similar problem, but the example isn't very minimal at all.

Let's assume we have the following things (as a minimum example):

  1. A Database Table containing some N columns. One of these columns contains 3 different values. Let's say it's an integer column that can contain 0, 1, or 2.
  2. A DataGridView that has been bound to said Database Table as a Data Source.

By itself, VB.NET would generate a textbox column. As this isn't very user-friendly, I'd like to replace it with let's say a DataGridComboBoxColumn. How would one go about doing this?

braX
  • 11,506
  • 5
  • 20
  • 33
aphid
  • 1,135
  • 7
  • 20

1 Answers1

0

As described in this answer, I managed to create a combo box column and populate the 'options' that it has with the following example data (listed here in JSON format);

[{"id": 0, "type": "never"},
 {"id": 1, "type": "always"}, 
 {"id": 2, "type": "sometimes"}]

Suppose we bind the data like this (myOptions is some helper class that returns the data above, as two class members, id and type). Assume that the database table column name is 'hasOption':

myDataGridView.Columns.Remove("hasOption")
Dim newColumn As New DataGridViewComboBoxColumn()
newColumn.DataSource = myOptions.GetChoices()
newColumn.DataPropertyName = "hasOption"
newColumn.ValueMember = "id"
newColumn.DisplayMember = "type"
newColumn.Name = "hasOption"
myDataGridView.Columns.Add(newColumn)

The important facts here are:

DataPropertyName must exactly match the data source column name (database table column name)

ValueMember must match the name of the column that contains the values (IDs) for the combo box options.

DisplayMember must match the name of the column that contains the displayed text to the user for the combo box options.

When using a list of options (manual data, so the combo box options are not stored in the database, these option members (id and type in the example) have to be implemented as properties. Merely using public class members results in a strange error when adding the custom column to the DataGridView.

The value member class is implemented like so:

Public Class myOptions
Public Property type As String
Public Property id As Integer
Public Sub New(ByVal nType As String, ByVal nID As Integer)
    type = nType
    id = nID
End Sub

Private Shared allValues As List(Of myOptions) = New List(Of myOptions) From {
    New myOptions("never", 0),
    New myOptions("always", 1),
    New myOptions("sometimes", 2)
}

Public Shared Function GetChoices() As List(Of myOptions)
    Return allValues
End Function
End Class
aphid
  • 1,135
  • 7
  • 20