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
Try
'get the row that is currently displayed at top
hti = DataGrid1.HitTest(point0-0)
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
MsgBox(ex.Message)
End Try
Else
FirstLoad = False
End If
End Sub
Finally, in the code where you populate your datagrid:
'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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.