Click here to Skip to main content
11,927,190 members (48,003 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

Adjusting DataGridTableStyle for IList objects

, 20 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Setting the member name property for a DataGridTableStyle for an IList object.


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
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, _
                ByVal e As system.EventArgs) Handles MenuItem2.Click
        ' Fill data
        mlstPersons.Add(New person("John", 39))
        mlstPersons.Add(New person("Jane", 33))

        DataGrid1.DataSource = mlstPersons

    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

        cs = New DataGridTextBoxColumn
        cs.HeaderText = "aGe"
        cs.MappingName = "age"
        cs.Width = 50

        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
            Return mName
        End Get
        Set(ByVal value As String)
            mName = value
        End Set
    End Property

    Public Property age() As Integer
            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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Lex Beekmans
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Ayelet Dahan11-Jul-10 0:45
memberAyelet Dahan11-Jul-10 0:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 20 Mar 2009
Article Copyright 2009 by Lex Beekmans
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid