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 来实现 发现总绑定不上数据 ...
随机推荐
- php中mb_strlen,mb_substr根据中文长度截取字符串
大于8截取,小于等于则不截取. 结合thinkphp模板引擎规则,代码如下: <,,'utf-8'}..<else/>{sh:$vo.name}</if> 这里if中的函 ...
- thread_local变量
thread_local变量是C++ 11新引入的一种存储类型.它会影响变量的存储周期(Storage duration),C++中有4种存储周期: automatic static dynamic ...
- 使用QuartZ.net来做定时计划任务 ; 值不能为 null。 参数名: assemblyString
1. 当异常的时候, 发现需要的类名, 没有取到, 然后就发生异常了 2. 分析: 业务层调用数据层, 数据层去掉配置的时候, 发现配置文件中根本就没有配置, 这个时候使用反射来取, 肯定是拿不到的, ...
- oracle显示数据库名和表名
oracle查看表名 select table_name from user_tables; select table_name from dba_tables; select * from all_ ...
- python's fourteenth day for me 内置函数
locals: 函数会以字典的类型返回当前位置的全部局部变量. globals: 函数会以字典的了类型返回全部的全局变量. a = def func(): b = print(locals()) ...
- 浅谈 Python 的 with 语句 【转载】
引言 with 语句是从 Python 2.5 开始引入的一种与异常处理相关的功能(2.5 版本中要通过 from __future__ import with_statement 导入后才可以使用) ...
- Linux字符设备驱动实现
Linux字符设备驱动实现 要求 编写一个字符设备驱动,并利用对字符设备的同步操作,设计实现一个聊天程序.可以有一个读,一个写进程共享该字符设备,进行聊天:也可以由多个读和多个写进程共享该字符设备,进 ...
- 说说API的重放机制
API的重放机制 我们在设计接口的时候,最怕一个接口被用户截取用于重放攻击.重放攻击是什么呢?就是把你的请求原封不动地再发送一次,两次...n次,一般正常的请求都会通过验证进入到正常逻辑中,如果这个正 ...
- Codeforces 1120D (树形DP 或 最小生成树)
题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...
- SpringBoot23 分模块开发
1 开发环境说明 JDK:1.8 MAVEN:3.5 IDEA:2017.2.5 SpringBoot:2.0.3.RELEASE 2 创建SpringBoot项目 2.1 项目信息 2.2 添加项目 ...