实现的方式有好几种。之前使用的是下面这种在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. nacos 实战(史上最全)

    文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...

  2. Java8的Stream API确实很牛,但性能究竟如何?

    Stream Performance 已经对 Stream API 的用法鼓吹够多了,用起简洁直观,但性能到底怎么样呢?会不会有很高的性能损失?本节我们对 Stream API 的性能一探究竟. 为保 ...

  3. ES6、ES7的一些新特性

    1.常见的就是let 和 const 命令 let 只在命令所在的代码块内有效 const声明一个只读的常量 2.变量的赋值 let [a, b, c] = [1, 2, 3]; 这样输出的话a=1, ...

  4. 服务器硬件及RAID配置实战

    一.RAID磁盘阵列介绍 二.阵列卡介绍 三.阵列卡的缓存 四.实验构建软RAID磁盘阵列 一.RAID磁盘阵列介绍 1.定义 是Redundant Array of Independent Disk ...

  5. 使用阿里云服务器部署jupyter notebook远程访问

    安装annaconda 与jupyter notebook annaconda在已经自带了jupyter notebook.jupyter lab.ipython 等一系列工具,不需要再单独安装这些工 ...

  6. Redis 雪崩、穿透、击穿、并发、缓存讲解以及解决方案

    1.缓存雪崩 数据未加载到缓存中,或者缓存同一时间大面积的失效,从而导致所有请求都去查数据库,导致数据库CPU和内存负载过高,甚至宕机. 比如一个雪崩的简单过程 1.redis集群大面积故障 2.缓存 ...

  7. hdu 1754 I Hate It 线段树 单点更新 区间最值

    线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...

  8. Binding(五):多路绑定

    Binding不止能绑定一个源,它还能绑定多个源,这就是我们这节要讲的多路绑定:MultiBinding. 使用多路绑定跟一般的绑定还是有区别的,首先它并不能很好的在标记扩展中使用,另外,使用多路绑定 ...

  9. CentOS-yum安装Nginx

    查看系统版本 $ cat /etc/redhat-release Nginx 不在默认的 yum 源中,使用官网的 yum 源 $ rpm -ivh http://nginx.org/packages ...

  10. 解决Windows Server 2012 在VMware ESXi中经常自动断网问题

    最近一些开发人员反映他们使用的 Windows server2012 R2 虚拟机过段时间就远程连接不上了,ping也不通(已关闭防火墙),我们登录ESXi发现,Windows Server 的网络图 ...