定义两个样式对象:

 //定义两种行样式
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自定义行样式和行标题的更多相关文章

  1. 自定义bootstrap样式-9行样式自定义漂亮大气bootstrap导航栏

    有人说前端发展太快,框架太多,各有所需,各有所长.看看这幅图,估计都知道这些框架,但是大部分公司中实际要用到的也就那么几个. 发展再快,框架再多.还是得回到原点,不就是Html+Css+JavaScr ...

  2. Winform自定义窗体样式,实现标题栏可灵活自定义

    最近在编写C/S结构应用程序时,感觉窗体的标题栏样式太死板了,标题文字不能更改大小.颜色.字体等,按钮不能隐藏等问题,在网上也查找了许多相关的资料,没有找到合适的解决方案,发现许多人也在寻求这个问题, ...

  3. DataGridView列的宽度、行的高度自动调整

    注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. 介绍一下DataGridView列的宽度和行的高度,根据单元格或Header的内容(一般是内容全部被表示)自 ...

  4. c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行

    设置DataGridView的属性SelectionMode为FullRowSelect 这样就使DataGridView不是选择一个字段,而是选择一整行了 设置DataGridView的属性Mult ...

  5. 转:DataGridView列的宽度、行的高度自动调整

    注意:DataGridView控件是从.NET Framework 2.0版本开始追加的. 介绍一下DataGridView列的宽度和行的高度,根据单元格或Header的内容(一般是内容全部被表示)自 ...

  6. [简单]poi word2007表格按模版样式填充行数据

    主要实现了按照模版行的样式填充数据,针对的是动态数据,静态数据可以直接替换变量实现,先说下缺点:1)暂未实现特殊样式填充(如列合并(跨行合并)),只能用于普通样式(如段落间距 缩进 字体 对齐)2)数 ...

  7. C# DevExpress_gridControl 行号行样式

    #region 行号 /// <summary> /// 行号 /// </summary> /// <param name="sender"> ...

  8. bootstrap-table 回显选中行,行样式

    { filed:'status', checkbox:true, formatter:function(value,row,index){ if (row.status == 1) //根据行里字段判 ...

  9. WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据

    原文:WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据 功能阐述 就上面那图片 刚开始 考虑使用 RowHeaderTemplate 来实现  发现总绑定不上数据  ...

随机推荐

  1. 【转】Jmeter:图形界面压力测试工具

    Jmeter是一款强大的图形界面压力测试工具,完全用Java写成,关于Jmeter的介绍,网上其实有不少的文章,我原本是不想再重复写类似文章的,但我发现有些很关键性的,在我们测试中一定会用到的一些设置 ...

  2. java中的getProperty()方法。获取系统中属性名为key的属性对应的值

    总结:getProperty方法:获取系统中属性名为key的属性对应的值,系统中常见的属性名以及属性如下: 现在用getProperty()的方法,获取系统信息代码: package com.aaa; ...

  3. Oracle 复杂查询(1)

    一.复杂查询 1. 列出至少有一个员工的所有部门编号.名称,并统计出这些部门的平均工资.最低工资.最高工资. 1.确定所需要的数据表: emp表:可以查询出员工的数量: dept表:部门名称: emp ...

  4. VS2017中的附加到进程

    vs2017 里加入了IIS Express,所以在web项目执行的时候,在附加进程中找不到端口号了,只能找到PID 但是浏览器的地址栏里还是带有端口的地址:http://localhost:1351 ...

  5. JavaScript高级程序设计学习

    1.变量 变量使用var操作符定义,var message,定义一个message变量,可用来保存任何类型的变量.未经初始化的变量值为undifided: 如果没变量没有被var定义,那么被执行后会成 ...

  6. Burpsuite模块—-Intruder模块详解

    一.简介 Burp Intruder是一个强大的工具,用于自动对Web应用程序自定义的攻击,Burp Intruder 是高度可配置的,并被用来在广范围内进行自动化攻击.你可以使用 Burp Intr ...

  7. @Value关于static字段的注入

    @Component public class BaseCode { //应用key public static String APP_KEY; //应用密钥 public static String ...

  8. Monthly Expense(二分--最小化最大值)

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  9. java基础之多线程五:实现Runnable的原理

    实现Runnable接口的原理. 背景: 多线程的第一种实现方式是::继承Thread类, 因为我们自定义的类(MyThread)是Thread类的子类, 所以MyThread类的对象调用start( ...

  10. 【COCI2012】覆盖字符串

    [题目描述] 给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串.现在Mirko要用这M个字符串去覆盖给出的那个字符串的.覆盖时,必须保证:1.Mirko的字符串不能拆开,旋转:2 ...