Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFormat the columns displayed by a DataTable in a DataGrid control in Visual Basic .NET
DescriptionThis example shows how to format the columns displayed by a DataTable in a DataGrid control in Visual Basic .NET in Visual Basic .NET.
KeywordsDataTable, column, format, align, alignment, DataGrid, VB.NET
CategoriesVB.NET, Controls, Database, Strings
 
The following code makes a DataTable, defines its columns, and then adds data to it. Next it creates a DataGridTableStyle object to define styles for the DataGrid. It sets the object's MappingName property so the DataGrid knows which table it contains is the one to which it should apply the styles.

The program then creates a DataGridBoolColumn object to define the table's first column. It makes DataGridTextBoxColumn objects to define the table's other columns. It sets the HorizontalAlignment properties to tell the control how to align the columns. It sets the Price column's Format property to "C" to format this column as currency. The code also sets column headers for all of the columns.

The code finishes by adding the DataGridTableStyle object to the DataGrid's TableStyles collections, assigning the DataTable to the DataGrid's DataSource property, and hiding the grid's caption.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make the DataTable and give it columns.
    Dim dt As New DataTable("Purchases")
    dt.Columns.Add("Selected", GetType(Boolean))
    dt.Columns.Add("Item", GetType(String))
    dt.Columns.Add("Quantity", GetType(Integer))
    dt.Columns.Add("Price", GetType(Single))

    ' Add data to the DataTable.
    dt.Rows.Add(New Object() {True, "Donut", 12, 2.37})
    dt.Rows.Add(New Object() {True, "CD-RW Disks", 20, _
        16.95})
    dt.Rows.Add(New Object() {False, "Laptop Computer", 1, _
        995.0})
    dt.Rows.Add(New Object() {True, "Cookie", 1440, 37.5})

    ' Make a DataGridTableStyle to map this DataTable.
    Dim table_style As New DataGridTableStyle
    table_style.MappingName = dt.TableName

    ' The first column is a Boolean.
    Dim selected_style As New DataGridBoolColumn
    selected_style.Alignment = HorizontalAlignment.Right
    selected_style.MappingName = "Selected"
    selected_style.HeaderText = "Selected?"
    selected_style.AllowNull = False
    table_style.GridColumnStyles.Add(selected_style)

    ' The second column is a string.
    Dim item_style As New DataGridTextBoxColumn
    item_style.Alignment = HorizontalAlignment.Left
    item_style.MappingName = "Item"
    item_style.HeaderText = "Descr"
    table_style.GridColumnStyles.Add(item_style)

    ' The third column is an integer.
    Dim quantity_style As New DataGridTextBoxColumn
    quantity_style.Alignment = HorizontalAlignment.Right
    quantity_style.MappingName = "Quantity"
    quantity_style.HeaderText = "Qty"
    table_style.GridColumnStyles.Add(quantity_style)

    ' The fourth column is currency.
    Dim price_style As New DataGridTextBoxColumn
    price_style.Format = "c"
    price_style.Alignment = HorizontalAlignment.Right
    price_style.MappingName = "Price"
    price_style.HeaderText = "Cost"
    table_style.GridColumnStyles.Add(price_style)

    ' Add the DataGridTableStyle to the DataGrid
    ' so it knows how to map this table.
    DataGrid1.TableStyles.Add(table_style)

    ' Attach the DataTable to the DataGrid.
    DataGrid1.DataSource = dt

    ' Don't show a table caption.
    DataGrid1.CaptionVisible = False
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated