Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ


How to set up DataGrid Columns programatically? by PankajBanga
Posted: 16 Jul 03

General rule of thumb for Adding Tables and Columns to the Windows Forms DataGrid control:

Note: when programmatically specifying column styles, always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection object before adding DataGridTableStyle object to the GridTableStylesCollection object.

1.    In order to display data in the table, you must first bind the DataGrid control to a DataSet.
2.    Declare new table style and set its mapping name.
3.    Declare a new column style and set its mapping name and other properties.
4.    Call the Add method of GridCoulmnStylesCollection object to add the column to the table style.
5.    Call the Add method of the GridTableStylesCollection object to add the table style to the data grid.

Follow this simple sample:-

Imports System.Data.OleDb

        Dim strConn As String
        Dim strSQL As String
        Dim Conn As OleDbConnection

        Dim objDA As OleDbDataAdapter
        Dim objDS As New DataSet()

            'Build the Connection strings.    
            strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                      "Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Prospects.mdb"

            'Pass the Connection string to OleDbConnection object.
            Conn = New OleDbConnection(strConn)

            'Build the SQL strings.    
            strSQL = "SELECT SubTypeID, SubTypeName " & _
                     "FROM SubType ORDER BY SubTypeName"

            'Initialize the OleDbDataAdapter with SQL and Connection string,
            'and then use the OleDbAdapter to fill the DataSet with data.
            objDA = New OleDbDataAdapter(strSQL, Conn)
            objDA.Fill(objDS, "SubType")

            'Bind the DataSet to DataGrid.
            DataGrid1.SetDataBinding(objDS, "SubType")

            'Create DataGridTableStyle and map it to the data table.
            Dim subtypeTableStyle As New DataGridTableStyle()
            subtypeTableStyle.MappingName = "SubType"

            'Create ColumnStyle and set its mapping name & other properties.
            Dim colSubTypeID As New DataGridTextBoxColumn()
            colSubTypeID.HeaderText = "SubTypeID"
            colSubTypeID.MappingName = "SubTypeID"
            colSubTypeID.Width = 0

            Dim colSubTypeName As New DataGridTextBoxColumn()
            colSubTypeName.HeaderText = "Sub Type"
            colSubTypeName.MappingName = "SubTypeName"
            colSubTypeName.Width = 100

            'Call the Add method of GridColumnStylesCollection object to add the column to the table style.

            'Call the Add method of GridTbaleStylesCollection object to add the table style to the data grid.
            grdSubType.TableStyles("SubType").ReadOnly = True
            grdSubType.TableStyles("SubType").AllowSorting = False

        Catch Excep As System.Exception
            MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close