DataGridView自定义行样式和行标题
定义两个样式对象:
//定义两种行样式
private DataGridViewCellStyle m_RowStyleNormal;
private DataGridViewCellStyle m_RowStyleAlternate;
在窗体加载的时候对样式进行设置:
/// <summary>
/// 设置行样式
/// </summary>
private void SetRowStyle()
{
//可根据需要设置更多样式属性,如字体、对齐、前景色、背景色等
this.m_RowStyleNormal = new DataGridViewCellStyle();
this.m_RowStyleNormal.BackColor = Color.LightBlue;
this.m_RowStyleNormal.SelectionBackColor = Color.LightSteelBlue;
this.m_RowStyleAlternate = new DataGridViewCellStyle();
this.m_RowStyleAlternate.BackColor = Color.LightGray;
this.m_RowStyleAlternate.SelectionBackColor = Color.LightSlateGray;
}
定义演示数据:
/// <summary>
/// 绑定数据
/// </summary>
private void BindData()
{
//建立一个DataTable并填充数据,然后绑定到DataGridView控件上
m_GradeTable = new DataTable();
m_GradeTable.Columns.Add("Class", typeof(string));
m_GradeTable.Columns.Add("Name", typeof(string));
m_GradeTable.Columns.Add("Grade", typeof(int));
m_GradeTable.Rows.Add(new string[] { "Class1", "Jim", "" });
m_GradeTable.Rows.Add(new string[] { "Class1", "Jack", "" });
m_GradeTable.Rows.Add(new string[] { "Class1", "Bill", "" });
m_GradeTable.Rows.Add(new string[] { "Class2", "Tom", "" });
m_GradeTable.Rows.Add(new string[] { "Class2", "Rose", "" });
m_GradeTable.Rows.Add(new string[] { "Class3", "Peter", "" });
m_GradeTable.Rows.Add(new string[] { "Class3", "David", "" });
m_GradeTable.Rows.Add(new string[] { "Class3", "Eric", "" });
m_GradeTable.Rows.Add(new string[] { "Class3", "Lily", "" });
this.bdsGrade.DataSource = m_GradeTable;
}
在DataGridView控件的CellFormatting事件中实现设置行样式、单元格样式和行号:
private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//在此对行样式进行设置 if (e.ColumnIndex == this.dgvGrade.Columns["ColumnClass"].Index)//根据班级设置行样式
{
DataGridViewRow CurrentRow = this.dgvGrade.Rows[e.RowIndex];
CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + );//显示行号,也可以设置成显示其他信息
//CurrentRow.HeaderCell.ToolTipText = "当前第" + Convert.ToString(e.RowIndex + 1) + "行";//设置ToolTip信息
//以下为根据上一行内容判断所属组的效果
if (e.RowIndex == )//首行必须特殊处理,将其设置为常规样式
{
CurrentRow.DefaultCellStyle = this.m_RowStyleNormal;
}
else
{
//判断和上一行是否属于同一个班级,如果是则设置相同样式,否则设置另一种样式
//需要定义两个DataGridViewCellStyle,用于交替显示,也可以根据需要隐藏一些和上一行重复的信息
//这里当两行是同一个班级时,将下一行的班级信息隐藏掉,选中时则显示班级信息
if (CurrentRow.Cells[e.ColumnIndex].Value != DBNull.Value && CurrentRow.Cells[e.ColumnIndex].Value != null
&& CurrentRow.Cells[e.ColumnIndex].Value.ToString() == this.dgvGrade.Rows[e.RowIndex - ].Cells[e.ColumnIndex].Value.ToString())
{
CurrentRow.DefaultCellStyle = this.dgvGrade.Rows[e.RowIndex - ].DefaultCellStyle;//设置和上一行的样式相同
CurrentRow.Cells[e.ColumnIndex].Style.ForeColor = CurrentRow.DefaultCellStyle.BackColor;//用前景色隐藏信息
//如果需要选中时显示完整信息则注释该下面一行
//CurrentRow.Cells[e.ColumnIndex].Style.SelectionForeColor = CurrentRow.DefaultCellStyle.SelectionBackColor;//选中时也使前景色等于背景色,将文字隐藏掉
}
else//当前行和上一行不属于同一个班级时
{
if (this.dgvGrade.Rows[e.RowIndex - ].DefaultCellStyle == this.m_RowStyleNormal)//根据上一行的样式设置当前行的样式
CurrentRow.DefaultCellStyle = this.m_RowStyleAlternate;
else
CurrentRow.DefaultCellStyle = this.m_RowStyleNormal;
}
}//if(e.RowIndex == 0)
}
else if (e.ColumnIndex == this.dgvGrade.Columns["ColumnGrade"].Index)//根据成绩设置单元格样式
{
if (this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != DBNull.Value
&& Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) < )//对不及格的成绩设置特殊样式
{
this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
}
}
}
在DataGridView控件的RowPostPaint事件中实现行标题图标绘制和提示信息设置:
//根据内容设置行标头
private void dgvDataTable_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value == DBNull.Value)
return;
int intGrade = Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value);//获取成绩
Image RowIcon;//标头图标
string strToolTip;//提示信息
if (intGrade >= )
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeA;//从资源文件中获取图片
strToolTip = "Grade A";
}
else if (intGrade >= )
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeB;
strToolTip = "Grade B";
}
else if (intGrade >= )
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeC;
strToolTip = "Grade C";
}
else if (intGrade >= )
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeD;
strToolTip = "Grade D";
}
else
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeF;
strToolTip = "Grade F";
}
e.Graphics.DrawImage(RowIcon, e.RowBounds.Left + this.dgvGrade.RowHeadersWidth - , e.RowBounds.Top + , , );//绘制图标
this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText = strToolTip;//设置提示信息
}
DataGridView自定义行样式和行标题的更多相关文章
- 自定义bootstrap样式-9行样式自定义漂亮大气bootstrap导航栏
有人说前端发展太快,框架太多,各有所需,各有所长.看看这幅图,估计都知道这些框架,但是大部分公司中实际要用到的也就那么几个. 发展再快,框架再多.还是得回到原点,不就是Html+Css+JavaScr ...
- Winform自定义窗体样式,实现标题栏可灵活自定义
最近在编写C/S结构应用程序时,感觉窗体的标题栏样式太死板了,标题文字不能更改大小.颜色.字体等,按钮不能隐藏等问题,在网上也查找了许多相关的资料,没有找到合适的解决方案,发现许多人也在寻求这个问题, ...
- DataGridView列的宽度、行的高度自动调整
注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. 介绍一下DataGridView列的宽度和行的高度,根据单元格或Header的内容(一般是内容全部被表示)自 ...
- c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行
设置DataGridView的属性SelectionMode为FullRowSelect 这样就使DataGridView不是选择一个字段,而是选择一整行了 设置DataGridView的属性Mult ...
- 转:DataGridView列的宽度、行的高度自动调整
注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. 介绍一下DataGridView列的宽度和行的高度,根据单元格或Header的内容(一般是内容全部被表示)自 ...
- [简单]poi word2007表格按模版样式填充行数据
主要实现了按照模版行的样式填充数据,针对的是动态数据,静态数据可以直接替换变量实现,先说下缺点:1)暂未实现特殊样式填充(如列合并(跨行合并)),只能用于普通样式(如段落间距 缩进 字体 对齐)2)数 ...
- C# DevExpress_gridControl 行号行样式
#region 行号 /// <summary> /// 行号 /// </summary> /// <param name="sender"> ...
- bootstrap-table 回显选中行,行样式
{ filed:'status', checkbox:true, formatter:function(value,row,index){ if (row.status == 1) //根据行里字段判 ...
- WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据
原文:WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据 功能阐述 就上面那图片 刚开始 考虑使用 RowHeaderTemplate 来实现 发现总绑定不上数据 ...
随机推荐
- 微服务监控之二:Metrics+influxdb+grafana构建监控平台
系统开发到一定的阶段,线上的机器越来越多,就需要一些监控了,除了服务器的监控,业务方面也需要一些监控服务.Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作. 使 ...
- php判断是否为ajax请求
先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest 在后端可以 ...
- 微信企业号支付个人php实现
导语:分销商,微商提现怎么提? 直接用微信支付. 实现如下: 微信支付配置 /*微信支付*/ 'PAY_WEIXIN' => array( 'appid' => 'XXXX', 'apps ...
- python开发mysql:索引
一,索引管理 索引分类: 普通索引INDEX:加速查找 唯一索引: -主键索引PRIMARY KEY:加速查找+约束(不为空.不能重复) -唯一索引UNIQUE:加速查找+约束(不能重复) 联合索引: ...
- 代码说明call和apply方法的区别 (咱们这方面讲解的少,这样的题有变式,需要举例讲解一下)
这两个都是函数的方法,可以改变this的指向,fn.call(obj,param1,param2,…) call传入单个参数 fn.apply(obj,[param1,param2,...]) app ...
- java成神之——properties,lambda表达式,序列化
Properties 加载defaults.properties文件 写Properties到xml文件 读Properties从xml文件 Lambda表达式 自定义 内置 sort方法中使用Lam ...
- springboot成神之——swagger文档自动生成工具
本文讲解如何在spring-boot中使用swagger文档自动生成工具 目录结构 说明 依赖 SwaggerConfig 开启api界面 JSR 303注释信息 Swagger核心注释 User T ...
- DFS leetcode
把字符串转换成整数 class Solution { public: int StrToInt(string str) { int n = str.size(), s = 1; long long r ...
- JS||JQUERY常用语法
cookieEnabled属性语法 通常可以在浏览器的临时文件夹中保存一个文件,此文件可以包含用户信息(比如浏览过什么页面,是否选择了自动登录)等,这个文件被称作cookie,通过cookieEnab ...
- leetcode594
public class Solution { public int FindLHS(int[] nums) { Dictionary<int, int> dic = new Dictio ...