在dataGridView中显示行号(VS2005)

 //在DataGridView中显示行号,需要要处理DataGridView的RowPostPaint事件:
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush B 
= new SolidBrush(Color.Red);
            
//or:SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor)

            e.Graphics.DrawString(Convert.ToString(e.RowIndex 
+ 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, B, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            
//or:e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
           
            
//SolidBrush用于定义单色画笔。画笔用于填充图形形状,如:矩形、椭圆、扇型、多边型和封闭路径。此类无法继承。
            
//SolidBrush的构造函数:=new SolidBrush(Color color)
            /*DrawString方法:DrawString(string s,Font font,Brush brush,float x,float y)
                     s:要绘制的字符串
                     f:定义字符串的文本格式
             brush:确定所绘制的文本的颜色和纹理
                     x:左上角的x坐标
                     y:左上角的y坐标
             
*/
            
//System.Globalization.CultureInfo.CurrentUICulture:提供区域性特定的格式设置信息
        }
原文地址:https://www.cnblogs.com/perfect/p/571090.html