微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐