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 来实现 发现总绑定不上数据 ...
随机推荐
- Oracle实例 startup 报错:LRM-00109: could not open parameter file
SQL> startup ORA-01078: failure in processing system parameters LRM-00109: could not open paramet ...
- git 本地文件里内容 操作记录
本地环境文件合并分支(以下的都分别 commit提交了的) [一.分支[追加] 和 [新增] 新信息 合并主线 情景] 分支内容: dr.find_element_by_id("su&qu ...
- MariaDB存在的问题
MySQL与MariaDB对嵌套的查询语句当中的order by的处理方法不同.MySQL会忠实执行内层查询的排序子句,但是MariaDB会将这个order by去掉,理论依据就是关系理论 --- 一 ...
- maven学习2
pom.xml文件中的内 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns= ...
- Dev DateEdit控件格式设置
设置日期显示格式: 设置三个属性(显示时.编辑时) dtPubDate.Properties.DisplayFormat.FormatString = "yyyy-MM-dd"; ...
- left join的多重串联与groupby
有三张表或组合查询,f1,f2,f3,其中,f1分别与f2,f3是一对多关系,f1一条记录可能对应f2或f3中0条或多条记录 要创建一个查询,以f1为基准,即f1中有多少条记录,结果也就返回对应数量的 ...
- jquery添加和删除多个同名的input输入框
<script type="text/javascript"> function del(obj){ $(obj).parents("li").re ...
- Java之泛型
1. 概述 在引入泛型之前,Java类型分为原始类型.复杂类型,其中复杂类型分为数组和类.引入泛型后,一个复杂类型就可以在细分成更多的类型. 例如原先的类型List ...
- Java面向对象-代码块
Java面向对象-代码块 代码块主要就是通过{}花括号 括起来的代码: 主要分为 普通代码块 构造块 静态代码块三类.后面学到线程还有一个同步代码块,到时候再说: 普通代码块:仅仅是花括号括起来的代码 ...
- HDFS的介绍
设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: 在大数据系统中作用:为各类分布式运算框架(如:mapreduce,spark,te ...