DataGridView 显示行号与背景颜色
实现的方式有好几种。之前使用的是下面这种在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 显示行号与背景颜色的更多相关文章
- Winform中的DatagridView显示行号
1.设置 RowPostPaint 为true 2.启用RowPostPaint事件 /// <summary> /// DataGridView显示行号 /// </summary ...
- DataGridView显示行号-RowPostPaint
DataGridView控件在显示数据时,我们有时候需要显示行号,以便检索查看方便使用. 但DataGridView默认没有设置显示行号的属性. 此时我们只要在DataGridView的RowPost ...
- DataGridView显示行号
//可以在DataGirdView的RowPostPaint事件中进行绘制. //如:添加以下方法代码 private void DrawRowIndex(object sender, DataGri ...
- C# DataGridView显示行号的三种方法
方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...
- DataGridView显示行号的几种方法来自http://www.soaspx.com/dotnet/csharp/csharp_20100204_2740.html
方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dataGridView1_RowPostPai ...
- 【转】DataGridView显示行号
ref:http://blog.csdn.net/xieyufei/article/details/9769631 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件 ...
- 让DataGridView显示行号
http://www.cnblogs.com/JuneZhang/archive/2011/11/21/2257630.html 为了表示行号,我们可以在DataGridView的RowP ...
- DataGridView大扩展——显示行号
原文 DataGridView大扩展——显示行号 在DataGridView 的实际使用中,经常需要标示出行号,这样可以比较醒目地看到当前信息.不过DataGridView 在绘制 DataGridV ...
- 修改vim的颜色主题 及显示行号
1.打开vim窗口,输入命令:color 或者colorscheme后回车查看当前颜色主题. 2. 输入:colorscheme <主题> 即可设置当前vim的颜色主题. sample: ...
随机推荐
- 在centos上安装docker
安装docker 卸载旧版本 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docke ...
- ES6中的Generator函数
今天小编发现一个es6中的新概念,同时也接触到了一个新关键字yeild,下面我就简单和大家聊聊es6中的generator函数.大家还可以关注我的微信公众号,蜗牛全栈. 一.函数声明:在functio ...
- 基于ABP落地领域驱动设计-02.聚合和聚合根的最佳实践和原则
目录 前言 聚合 聚合和聚合根原则 包含业务原则 单个单元原则 事务边界原则 可序列化原则 聚合和聚合根最佳实践 只通过ID引用其他聚合 用于 EF Core 和 关系型数据库 保持聚合根足够小 聚合 ...
- Docker与k8s的恩怨情仇(四)-云原生时代的闭源落幕
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 在本系列前几篇文章中,我们介绍了从Cloud Foundry到Docker等PaaS平台的发展迭代过程.今天 ...
- POJ 1696 Space Ant 点积计算夹角
题意: 一只特别的蚂蚁,只能直走或者左转.在一个平面上,有很多株植物,这只蚂蚁每天需要进食一株,这只蚂蚁从起点为(0,miny)的点开始出发.求最多能活多少天 分析: 肯定是可以吃到所有植物的,以当前 ...
- JDK1.8 ArrayList 源码解析
源码的解读逻辑按照程序运行的轨迹展开 Arraylist的继承&实现关系 打开ArrayList源码,会看到有如下的属性定义, ArrayList中定义的属性 /** * Default in ...
- Java Set HashSet
import java.util.HashSet; import java.util.Set; /** Set存储特点:数据无序.不可重复 Set接口的实现类: HashSet:Set接口的主要实现类 ...
- Mybatis:Mybatis 逆向工程 generator配置
一.使用Maven方式引入Mybatis依赖Jar包(版本号自己改或定义)
- 资源:mysql下载路径
mysql的下载路劲 https://dev.mysql.com/downloads/mysql/
- shiro框架整合ssm框架
下面我通过一个web的maven项目来讲解如何将shiro整合ssm框架,具体结构如下图 一.引入依赖的jar包 <?xml version="1.0" encoding=& ...