Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / Visual Basic

Adjusting DataGridTableStyle for IList objects

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
20 Mar 2009CPOL 22.7K   10   1
Setting the member name property for a DataGridTableStyle for an IList object.

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.

VB
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
        ' Fill data
        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

License

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


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Ayelet Dahan10-Jul-10 23:45
Ayelet Dahan10-Jul-10 23: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.