Click here to Skip to main content
Click here to Skip to main content

Adjusting DataGridTableStyle for IList objects

By , 20 Mar 2009
Rate this:
Please Sign up or sign in to vote.

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
        ' 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)

About the Author

Lex Beekmans

Netherlands Netherlands
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberAyelet Dahan10-Jul-10 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140421.2 | Last Updated 20 Mar 2009
Article Copyright 2009 by Lex Beekmans
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid