DataGridView自动显示行号(VB.NET)

如果想在DataGridView控件中自动添加行号,您可以在DataGridView控件CellPainting事件中添加相关代码。以下就是相关代码

Private Sub DataGridView1_CellPainting(ByVal sender As Object,ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

'判断条件是:满足行数索引号要大于或等于0 and 列数的索引号时小于0
If e.ColumnIndex < 0 And e.RowIndex >= 0 Then
e.Paint(e.ClipBounds,DataGridViewPaintParts.All)
Dim indexrect As Drawing.Rectangle = e.CellBounds
'定义显示的行号的坐标
indexrect.Inflate(-2,-2)
'绘画字符串的值
TextRenderer.DrawText(e.Graphics,(e.RowIndex + 1).ToString(),e.CellStyle.Font,indexrect,e.CellStyle.ForeColor,textformatFlags.Right)
e.Handled = True
End If

End Sub

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...