博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为DataGridView增加行号
阅读量:2446 次
发布时间:2019-05-10

本文共 1707 字,大约阅读时间需要 5 分钟。

     DataGridView本身没有行号,所以要想得到行号,需要对第一列进行重绘。我们可以在rowspostpaint事件中增加处理代码。

    ''' <summary>

    ''' 给GridView添加行号

    ''' </summary>

    ''' <param name="sender"></param>

    ''' <param name="e"></param>

    ''' <remarks></remarks>

    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles gridOrder.RowPostPaint

        GridNO.NO(DataGridView1, e)

    End Sub

Public Class GridNO

    Public Shared Sub NO(ByVal dv As DataGridView, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs)

        '在列前面加上行号

        '在DataGridView的RowPostPaint事件中这样调用:GridNo.NO(datagridview1,e)

 

        Dim Color As Color = dv.RowHeadersDefaultCellStyle.ForeColor

        If dv.Rows(e.RowIndex).Selected Then

            Color = dv.RowHeadersDefaultCellStyle.SelectionForeColor

        Else

            Color = dv.RowHeadersDefaultCellStyle.ForeColor

        End If

 

        Dim b As SolidBrush = New SolidBrush(Color)

        If e.RowIndex < 9 Then

            e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 22, e.RowBounds.Location.Y + 6)

        ElseIf e.RowIndex < 99 Then

            e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 19, e.RowBounds.Location.Y + 6)

        ElseIf e.RowIndex < 999 Then

            e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 16, e.RowBounds.Location.Y + 6)

        ElseIf e.RowIndex < 9999 Then

            e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 13, e.RowBounds.Location.Y + 6)

        Else

            e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 6)

        End If

        b.Dispose()

    End Sub

End Class

转载地址:http://nvxqb.baihongyu.com/

你可能感兴趣的文章
看HashMap源码前的必备冷知识,白话文式教学,适合刚开始了解源码的新手观看
查看>>
Spring-data-redis在shiro中的实例
查看>>
基于IBM大型主机,Linux开辟意大利旅游新天地(转)
查看>>
第一章(backup and recovery 笔记)
查看>>
第六章(backup and recovery 笔记)
查看>>
叶节点到根节点的路径_节点路径模块
查看>>
如何查找公共子字符串长度_如何在C中查找字符串的长度
查看>>
地理位置api_如何使用地理位置API
查看>>
数据结构设计 数据字典_Go数据结构:字典
查看>>
node_modules文件夹的大小不是问题。 这是一种特权
查看>>
dom 删除所有子元素_如何从DOM元素中删除所有子级
查看>>
html 打印样式控制_如何使用样式打印HTML
查看>>
gatsby_Next.js vs Gatsby vs create-react-app
查看>>
掌握React.Memo
查看>>
golang 延迟_了解Go中的延迟
查看>>
react 组件样式_如何设置React组件的样式
查看>>
node.js 模块_如何创建Node.js模块
查看>>
centos上安装git_如何在CentOS 8上安装Git
查看>>
在JavaScript中优化switch语句
查看>>
如何在CentOS 8上安装Node.js
查看>>