通过处理DataGridView的RowPostPaint事件来显示行号
1
/**/
/// <summary>
2 /// 在DataGridView中显示行号,需要要处理DataGridView的RowPostPaint事件
3 /// 在datagridview的行标题单元格中绘制行号
4 /// </summary>
5 /// <param name="sender"></param>
6 /// <param name="e"></param>
7 private void dgvCmd_RowPostPaint( object sender, DataGridViewRowPostPaintEventArgs e)
8 {
9
10 // Paint the row number on the row header.
11 // The using statement automatically disposes the brush.
12 DataGridView dgv = (DataGridView)sender;
13 using (SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor))
14 {
15 e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + dgv.RowHeadersWidth / 2, e.RowBounds.Location.Y + (e.RowBounds.Height - e.InheritedRowStyle.Font.GetHeight()) / 2);
16 }
17 //SolidBrush用于定义单色画笔。画笔用于填充图形形状,如:矩形、椭圆、扇型、多边型和封闭路径。此类无法继承。
18 //SolidBrush的构造函数:=new SolidBrush(Color color)
19 /**//*DrawString方法:DrawString(string s,Font font,Brush brush,float x,float y)
20 s:要绘制的字符串
21 f:定义字符串的文本格式
22 brush:确定所绘制的文本的颜色和纹理
23 x:左上角的x坐标
24 y:左上角的y坐标
25 */
26 //System.Globalization.CultureInfo.CurrentUICulture:提供区域性特定的格式设置信息
27 }
28
29
2 /// 在DataGridView中显示行号,需要要处理DataGridView的RowPostPaint事件
3 /// 在datagridview的行标题单元格中绘制行号
4 /// </summary>
5 /// <param name="sender"></param>
6 /// <param name="e"></param>
7 private void dgvCmd_RowPostPaint( object sender, DataGridViewRowPostPaintEventArgs e)
8 {
9
10 // Paint the row number on the row header.
11 // The using statement automatically disposes the brush.
12 DataGridView dgv = (DataGridView)sender;
13 using (SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor))
14 {
15 e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + dgv.RowHeadersWidth / 2, e.RowBounds.Location.Y + (e.RowBounds.Height - e.InheritedRowStyle.Font.GetHeight()) / 2);
16 }
17 //SolidBrush用于定义单色画笔。画笔用于填充图形形状,如:矩形、椭圆、扇型、多边型和封闭路径。此类无法继承。
18 //SolidBrush的构造函数:=new SolidBrush(Color color)
19 /**//*DrawString方法:DrawString(string s,Font font,Brush brush,float x,float y)
20 s:要绘制的字符串
21 f:定义字符串的文本格式
22 brush:确定所绘制的文本的颜色和纹理
23 x:左上角的x坐标
24 y:左上角的y坐标
25 */
26 //System.Globalization.CultureInfo.CurrentUICulture:提供区域性特定的格式设置信息
27 }
28
29