欢迎加我的QQ群:193522571,一起来讨论、交流!

C# winform datagridview rowheader 添加行标题的方法

        #region 写行号事件
        //在DataGridView控件上选择RowPostPaint事件
        private void dgvJointList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ((DataGridView)sender).fillRowHeaderIndex(e);
        }
        #endregion
        #region DataGridView的RowPostPaint事件中使用的添加行标题的公用方法
        /// <summary>
        /// DataGridView的RowPostPaint事件中使用的添加行标题的公用方法
        /// </summary>
        /// <param name="dgv">DataGridView名</param>
        /// <param name="e">DataGridViewRowPostPaintEventArgs事件名</param>
        public static void fillRowHeaderIndex(this DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle
            (
                e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                dgv.RowHeadersWidth - 4,
                e.RowBounds.Height
            );
            TextRenderer.DrawText
            (
                e.Graphics,
                (e.RowIndex + 1).ToString(),
                dgv.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right
            );
        }
        #endregion

 

 名称说明
 1 DrawText(IDeviceContext, String, Font, Point, Color) 使用指定的设备上下文、字体和颜色在指定位置绘制指定文本。
 2 DrawText(IDeviceContext, String, Font, Rectangle, Color) 使用指定的设备上下文、字体和颜色在指定界限中绘制指定文本。
 3 DrawText(IDeviceContext, String, Font, Point, Color, Color) 使用指定的设备上下文、字体、颜色和背景色在指定位置绘制指定文本。
 4 DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags) 使用指定的设备上下文、字体、颜色和格式说明在指定位置绘制指定文本。
 5 DrawText(IDeviceContext, String, Font, Rectangle, Color, Color) 使用指定的设备上下文、字体、颜色和背景色在指定界限中绘制指定文本。
 6 DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) 使用指定的设备上下文、字体、颜色和格式说明在指定界限中绘制指定文本。
 7 DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags) 使用指定的设备上下文、字体、颜色、背景色和格式说明在指定位置绘制指定文本
 8 DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags) 使用指定的设备上下文、字体、颜色、背景色和格式说明在指定界限中绘制指定文本。
posted @ 2014-06-05 16:03  swtool  阅读(3603)  评论(0编辑  收藏  举报
欢迎加我的QQ群:193522571,一起来讨论、交流!