实现的方式有好几种。之前使用的是下面这种在RowPostPaint事件中实现,效率不高。每次改变控件尺寸时都会执行

private void MsgGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridView gdView = sender as DataGridView;
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
gdView.RowHeadersWidth - 4,
e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
gdView.RowHeadersDefaultCellStyle.Font,
rectangle,
gdView.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}

为了消除更新所带来的的闪屏问题,需要开启窗体和控件的双缓存,在窗体的构造函数中插入下面的代码。

private IGForm()
{
//设置窗体的双缓冲
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles(); InitializeComponent();
//
//利用反射设置DataGridView的双缓冲
Type dgvType = this.MsgGridView.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(this.MsgGridView, true, null);
}

===========================================

显示DataGridView背景颜色

//单元格样式的BackColor方法设置背景色
DataGridView.RowsDefaultCellStyle.BackColor = Color.LightSteelBlue;
//奇数行样式的BackColor方法设置
DataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.LightSteelBlue;

整合一下,封装到一个类中,方便调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing; namespace SimpleDataGridView
{
/// <summary>
/// 设置DataGridView的样式
/// </summary>
public class DataGridViewStyle
{
/// <summary>
/// 普通的样式
/// </summary>
public void DgvStyle1(DataGridView dgv)
{
//奇数行的背景色
dgv.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
dgv.AlternatingRowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.Blue;
dgv.AlternatingRowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
//默认的行样式
dgv.RowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
dgv.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
dgv.RowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.Blue;
//数据网格颜色
dgv.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
//列标题的宽度
dgv.ColumnHeadersHeight = 28;
}
/// <summary>
/// 凹凸样式
/// </summary>
/// 需要手动设置this.RowTemplate.DividerHeight = 2;
public void DgvStyle2(DataGridView dgv)
{
dgv.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Sunken;
//列标题的边框样式
dgv.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;
dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dgv.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
dgv.ColumnHeadersHeight = 28;
//行的边框样式
dgv.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;
dgv.DefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
dgv.RowTemplate.DividerHeight = 1;
//禁止当前默认的视觉样式
dgv.EnableHeadersVisualStyles = false;
} /// <summary>
/// 给DataGridView添加行号
/// </summary>
/// <param name="dgv"></param>
/// <param name="e"></param>
public static void DgvRowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
{
try
{
//添加行号
SolidBrush v_SolidBrush = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
int v_LineNo = 0;
v_LineNo = e.RowIndex + 1;
string v_Line = v_LineNo.ToString();
e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5);
}
catch (Exception ex)
{
MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败");
}
} }
}

DataGridView 显示行号与背景颜色的更多相关文章

  1. Winform中的DatagridView显示行号

    1.设置 RowPostPaint 为true 2.启用RowPostPaint事件 /// <summary> /// DataGridView显示行号 /// </summary ...

  2. DataGridView显示行号-RowPostPaint

    DataGridView控件在显示数据时,我们有时候需要显示行号,以便检索查看方便使用. 但DataGridView默认没有设置显示行号的属性. 此时我们只要在DataGridView的RowPost ...

  3. DataGridView显示行号

    //可以在DataGirdView的RowPostPaint事件中进行绘制. //如:添加以下方法代码 private void DrawRowIndex(object sender, DataGri ...

  4. C# DataGridView显示行号的三种方法

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...

  5. DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dataGridView1_RowPostPai ...

  6. 【转】DataGridView显示行号

    ref:http://blog.csdn.net/xieyufei/article/details/9769631 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件 ...

  7. 让DataGridView显示行号

          http://www.cnblogs.com/JuneZhang/archive/2011/11/21/2257630.html 为了表示行号,我们可以在DataGridView的RowP ...

  8. DataGridView大扩展——显示行号

    原文 DataGridView大扩展——显示行号 在DataGridView 的实际使用中,经常需要标示出行号,这样可以比较醒目地看到当前信息.不过DataGridView 在绘制 DataGridV ...

  9. 修改vim的颜色主题 及显示行号

    1.打开vim窗口,输入命令:color 或者colorscheme后回车查看当前颜色主题. 2. 输入:colorscheme <主题> 即可设置当前vim的颜色主题. sample: ...

随机推荐

  1. pytest xfail的使用

    @pytest.mark.xfail: 期望测试用例是失败的,但是不会影响测试用例的的执行; 如果测试用例执行失败的则结果是xfail(不会额外显示出错误信息); 如果测试用例执行成功的则结果是xpa ...

  2. Pytest测试框架入门到精通(一)

    Python测试框架之前一直用的是unittest+HTMLTestRunner,听到有人说Pytest很好用,所以这边给大家介绍一下Pytest的使用 pytest是一个非常成熟的全功能的Pytho ...

  3. 谁能干掉了if else

    很多人觉得自己写的是业务代码,按照逻辑写下去,再把公用的方法抽出来复用就可以了,设计模式根本就没必要用,更没必要学. 一开始的时候,我也是这么想,直到我遇到... 举个例子 我们先看一个普通的下单拦截 ...

  4. MIT6.828 Lab2 内存管理

    Lab2 0. 任务介绍 你将编写一个内存管理代码.主要分为两大部分.分别对物理内存和虚拟内存的管理. 对于物理内存,每次分配内存分配器会为你分配4096bytes.也称为一个页(在大部分操作系统中一 ...

  5. android动画系列

    Android 属性动画(Property Animation) 完全解析 (上 动画系列 - 传统View动画与Property动画基础及比较 [Android 基础]Animation 动画介绍和 ...

  6. js笔记4

    1.js数据类型分析 (1)基础类型:string.number.boolean.null.undefined (2)引用类型:object-->json.array... 2.点运算  xxx ...

  7. Android系统Bitmap内存分配原理与优化

    一.前言 笔者最近致力于vivo游戏中心稳定性维护,在分析线上异常时,发现有相当一部分是由OutOfMemory引起.谈及OOM,我们一般都会想到内存泄漏,其实,往往还有另外一个因素--图片,如果对图 ...

  8. 46、django工程(view)

    46.1.django view 视图函数说明: 1.http请求中产生两个核心对象: (1)http请求:HttpRequest对象. (2)http响应:HttpResponse对象. 2.vie ...

  9. 附加数据库出现 无法打开物理文件 操作系统错误 5:拒绝访问 SQL

    刚刚从公司的电脑上考到自己刚刚装好系统的笔记本上面,出现了问题: 无法打开物理文件 操作系统错误 5:拒绝访问 . 网上找了下解决方法: 找到需要导入的  mdf和ldf  修改它的权限为完全控制,不 ...

  10. Layui 关闭自己刷新父页面

    var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); window.parent.locati ...