Introduction
When the member name of a DataGridTableStyle
isn't correctly set, the DataGrid
object will always use the default layout generated automatically. When using a DataSet
, you can assign the known table name, but for IList
objects, it works slightly different. The name of an IList
is generated at runtime.
Using the code
I've added a simple code example which you can paste in your project. The trick is to get the name of the list object at runtime by using mlstPersons.GetType.Name
.
Public Class Form1
Private mlstPersons As New List(Of person)
Private Sub MenuItem1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem1.Click
Application.Exit()
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, _
ByVal e As system.EventArgs) Handles MenuItem2.Click
mlstPersons.Add(New person("John", 39))
mlstPersons.Add(New person("Jane", 33))
DataGrid1.DataSource = mlstPersons
DataGrid1.TableStyles.Clear()
gridstyle(mlstPersons.GetType.Name)
End Sub
Private Sub gridstyle(ByVal mappingname As String)
Dim ts As New DataGridTableStyle
Dim cs As DataGridColumnStyle
Dim i As Int16
ts.MappingName = mappingname
cs = New DataGridTextBoxColumn
cs.HeaderText = "nAmE"
cs.MappingName = "name"
cs.Width = 100
ts.GridColumnStyles.Add(cs)
cs = New DataGridTextBoxColumn
cs.HeaderText = "aGe"
cs.MappingName = "age"
cs.Width = 50
ts.GridColumnStyles.Add(cs)
i = DataGrid1.TableStyles.Add(ts)
End Sub
End Class
Public Class person
Private mName As String
Private mAge As Integer
Public Property name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Property age() As Integer
Get
Return mAge
End Get
Set(ByVal value As Integer)
mAge = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal age As Integer)
mName = name
mAge = age
End Sub
End Class