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
 
 
 
 
 
TitleMake a DataGridView display numbered row headers in Visual Basic .NET
DescriptionThis example shows how to make a DataGridView display numbered row headers in Visual Basic .NET.
KeywordsDataGridView, row headers, header, Visual Basic .NET, VB.NET
CategoriesControls, Database
 
This example builds a DataTable in memory and gives it some data. It then attaches the DataTable to the form's DataGridView control.

Next the program makes a DataGridViewCellStyle object that uses a yellow background. It sets the final column's header cell's style to this object. Finally the code disables visual header styles to make the header use the customized style.

The Form's Load event handler finishes by calling subroutine NumberAllRows to create the row headers. Then the DataGridView raises its RowsAdded or RowsRemoved events, it also calls NumberAllRows to renumber the row headers.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make a DataTable.
    Dim students_table As New DataTable("Students")

    ' Add columns.
    students_table.Columns.Add("FirstName", GetType(String))
    students_table.Columns.Add("LastName", GetType(String))
    students_table.Columns.Add("Quiz Average", _
        GetType(String))

    ' Make the combined FirstName/LastName unique.
    Dim first_last_columns() As DataColumn = { _
        students_table.Columns("FirstName"), _
        students_table.Columns("LastName") _
    }
    students_table.Constraints.Add( _
        New UniqueConstraint(first_last_columns))

    ' Make some contact data.
    students_table.Rows.Add(New Object() {"Art", "Ant", 75})
    students_table.Rows.Add(New Object() {"Bev", "Bug", 80})
    students_table.Rows.Add(New Object() {"Cid", "Cat", 97})
    students_table.Rows.Add(New Object() {"Deb", "Dove", _
        82})
    students_table.Rows.Add(New Object() {"Ed", "Eager", _
        67})
    students_table.Rows.Add(New Object() {"Fran", "Fix", _
        71})
    students_table.Rows.Add(New Object() {"Gus", "Gantry", _
        88})
    students_table.Rows.Add(New Object() {"Hil", "Harris", _
        63})

    ' Attach grdStudents to the DataTable.
    grdStudents.DataSource = students_table

    ' Make a style to turn the Quiz Average header yellow.
    Dim header_style As New DataGridViewCellStyle
    header_style.BackColor = Color.Yellow
    grdStudents.Columns(2).HeaderCell.Style = header_style

    ' Disable header visual styles.
    grdStudents.EnableHeadersVisualStyles = False

    NumberAllRows()
End Sub

Private Sub grdStudents_RowsAdded(ByVal sender As Object, _
    ByVal e As _
    System.Windows.Forms.DataGridViewRowsAddedEventArgs) _
    Handles grdStudents.RowsAdded
    NumberAllRows()
End Sub

Private Sub grdStudents_RowsRemoved(ByVal sender As Object, _
    ByVal e As _
    System.Windows.Forms.DataGridViewRowsRemovedEventArgs) _
    Handles grdStudents.RowsRemoved
    NumberAllRows()
End Sub
 
Subroutine NumberAllRows simply loops through the rows setting each one's HeaderCell.Value property.
 
Private Sub NumberAllRows()
    ' Add row headers.
    For i As Integer = 0 To grdStudents.Rows.Count - 1
        grdStudents.Rows(i).HeaderCell.Value = i.ToString()
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated