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!
  • Students Click Here

*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 (Microsoft) VB.NET FAQ


Put text in the Row Header of a DataGrid by jebenson
Posted: 14 Jul 06 (Edited 18 Oct 06)

This code puts text in the row headers of a datagrid, like the line numbers in Excel.

'declare this global to the form
Dim point0-0 As Point

Then, in the DataGrid's Paint event handler:

    Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
        Dim row As Integer
        Dim yChange As Integer
        Dim y As Integer
        Dim RowText As String
        Dim hti As DataGrid.HitTestInfo

        'this Static boolean keeps this Paint code from
        'executing when the form is first loaded.

        Static FirstLoad As Boolean = True

        If Not FirstLoad Then

                'get the row that is currently displayed at top
                hti = DataGrid1.HitTest(point0-0)

                row = hti.Row

                yChange = DataGrid1.GetCellBounds(row, 0).Height + 1
                y = DataGrid1.GetCellBounds(row, 0).Top + 2

                While (y <= DataGrid1.Height - yChange And row < ds.Tables("TableName").Rows.Count)
                    RowText = (row + 1).ToString
                    e.Graphics.DrawString(RowText, DataGrid1.Font, New SolidBrush(Color.Black), 12, y)
                    y += yChange
                    row += 1
                End While
            Catch ex As Exception
            End Try


            FirstLoad = False
        End If
    End Sub

Finally, in the code where you populate your datagrid:

            DataGrid1.DataSource = ds.Tables("TableName")

            point0-0 = New Point(DataGrid1.GetCellBounds(0, 0).X + 4, DataGrid1.GetCellBounds(0, 0).Y + 4)


            Dim ts As DataGridTableStyle

            ts = New DataGridTableStyle

            ts.MappingName = "TableName"


            'this is to make sure that the rowheader column is wide
            'enough to display the row numbers without
            'them "bleeding" into the first column of data
            'play with this a little to set it where you want

            DataGrid1.TableStyles(0).RowHeaderWidth = 75

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET 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