定义两个样式对象:

  1. //定义两种行样式
  2. private DataGridViewCellStyle m_RowStyleNormal;
  3. private DataGridViewCellStyle m_RowStyleAlternate;

在窗体加载的时候对样式进行设置:

  1. /// <summary>
  2. /// 设置行样式
  3. /// </summary>
  4. private void SetRowStyle()
  5. {
  6. //可根据需要设置更多样式属性,如字体、对齐、前景色、背景色等
  7. this.m_RowStyleNormal = new DataGridViewCellStyle();
  8. this.m_RowStyleNormal.BackColor = Color.LightBlue;
  9. this.m_RowStyleNormal.SelectionBackColor = Color.LightSteelBlue;
  10. this.m_RowStyleAlternate = new DataGridViewCellStyle();
  11. this.m_RowStyleAlternate.BackColor = Color.LightGray;
  12. this.m_RowStyleAlternate.SelectionBackColor = Color.LightSlateGray;
  13. }

定义演示数据:

  1. /// <summary>
  2. /// 绑定数据
  3. /// </summary>
  4. private void BindData()
  5. {
  6. //建立一个DataTable并填充数据,然后绑定到DataGridView控件上
  7. m_GradeTable = new DataTable();
  8. m_GradeTable.Columns.Add("Class", typeof(string));
  9. m_GradeTable.Columns.Add("Name", typeof(string));
  10. m_GradeTable.Columns.Add("Grade", typeof(int));
  11. m_GradeTable.Rows.Add(new string[] { "Class1", "Jim", "" });
  12. m_GradeTable.Rows.Add(new string[] { "Class1", "Jack", "" });
  13. m_GradeTable.Rows.Add(new string[] { "Class1", "Bill", "" });
  14. m_GradeTable.Rows.Add(new string[] { "Class2", "Tom", "" });
  15. m_GradeTable.Rows.Add(new string[] { "Class2", "Rose", "" });
  16. m_GradeTable.Rows.Add(new string[] { "Class3", "Peter", "" });
  17. m_GradeTable.Rows.Add(new string[] { "Class3", "David", "" });
  18. m_GradeTable.Rows.Add(new string[] { "Class3", "Eric", "" });
  19. m_GradeTable.Rows.Add(new string[] { "Class3", "Lily", "" });
  20. this.bdsGrade.DataSource = m_GradeTable;
  21. }

在DataGridView控件的CellFormatting事件中实现设置行样式、单元格样式和行号:

  1. private void dgvDataTable_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  2. {
  3. //在此对行样式进行设置
  4.  
  5. if (e.ColumnIndex == this.dgvGrade.Columns["ColumnClass"].Index)//根据班级设置行样式
  6. {
  7. DataGridViewRow CurrentRow = this.dgvGrade.Rows[e.RowIndex];
  8. CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + );//显示行号,也可以设置成显示其他信息
  9. //CurrentRow.HeaderCell.ToolTipText = "当前第" + Convert.ToString(e.RowIndex + 1) + "行";//设置ToolTip信息
  10. //以下为根据上一行内容判断所属组的效果
  11. if (e.RowIndex == )//首行必须特殊处理,将其设置为常规样式
  12. {
  13. CurrentRow.DefaultCellStyle = this.m_RowStyleNormal;
  14. }
  15. else
  16. {
  17. //判断和上一行是否属于同一个班级,如果是则设置相同样式,否则设置另一种样式
  18. //需要定义两个DataGridViewCellStyle,用于交替显示,也可以根据需要隐藏一些和上一行重复的信息
  19. //这里当两行是同一个班级时,将下一行的班级信息隐藏掉,选中时则显示班级信息
  20. if (CurrentRow.Cells[e.ColumnIndex].Value != DBNull.Value && CurrentRow.Cells[e.ColumnIndex].Value != null
  21. && CurrentRow.Cells[e.ColumnIndex].Value.ToString() == this.dgvGrade.Rows[e.RowIndex - ].Cells[e.ColumnIndex].Value.ToString())
  22. {
  23. CurrentRow.DefaultCellStyle = this.dgvGrade.Rows[e.RowIndex - ].DefaultCellStyle;//设置和上一行的样式相同
  24. CurrentRow.Cells[e.ColumnIndex].Style.ForeColor = CurrentRow.DefaultCellStyle.BackColor;//用前景色隐藏信息
  25. //如果需要选中时显示完整信息则注释该下面一行
  26. //CurrentRow.Cells[e.ColumnIndex].Style.SelectionForeColor = CurrentRow.DefaultCellStyle.SelectionBackColor;//选中时也使前景色等于背景色,将文字隐藏掉
  27. }
  28. else//当前行和上一行不属于同一个班级时
  29. {
  30. if (this.dgvGrade.Rows[e.RowIndex - ].DefaultCellStyle == this.m_RowStyleNormal)//根据上一行的样式设置当前行的样式
  31. CurrentRow.DefaultCellStyle = this.m_RowStyleAlternate;
  32. else
  33. CurrentRow.DefaultCellStyle = this.m_RowStyleNormal;
  34. }
  35. }//if(e.RowIndex == 0)
  36. }
  37. else if (e.ColumnIndex == this.dgvGrade.Columns["ColumnGrade"].Index)//根据成绩设置单元格样式
  38. {
  39. if (this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != DBNull.Value
  40. && Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) < )//对不及格的成绩设置特殊样式
  41. {
  42. this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;//设置小于60的数字显示为红色
  43. this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.SelectionForeColor = Color.Red;
  44. this.dgvGrade.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  45. }
  46. }
  47. }

在DataGridView控件的RowPostPaint事件中实现行标题图标绘制和提示信息设置:

  1. //根据内容设置行标头
  2. private void dgvDataTable_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  3. {
  4. if (this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value == DBNull.Value)
  5. return;
  6. int intGrade = Convert.ToInt32(this.dgvGrade.Rows[e.RowIndex].Cells["ColumnGrade"].Value);//获取成绩
  7. Image RowIcon;//标头图标
  8. string strToolTip;//提示信息
  9. if (intGrade >= )
  10. {
  11. RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeA;//从资源文件中获取图片
  12. strToolTip = "Grade A";
  13. }
  14. else if (intGrade >= )
  15. {
  16. RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeB;
  17. strToolTip = "Grade B";
  18. }
  19. else if (intGrade >= )
  20. {
  21. RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeC;
  22. strToolTip = "Grade C";
  23. }
  24. else if (intGrade >= )
  25. {
  26. RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeD;
  27. strToolTip = "Grade D";
  28. }
  29. else
  30. {
  31. RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeF;
  32. strToolTip = "Grade F";
  33. }
  34. e.Graphics.DrawImage(RowIcon, e.RowBounds.Left + this.dgvGrade.RowHeadersWidth - , e.RowBounds.Top + , , );//绘制图标
  35. this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText = strToolTip;//设置提示信息
  36. }

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. php中mb_strlen,mb_substr根据中文长度截取字符串

    大于8截取,小于等于则不截取. 结合thinkphp模板引擎规则,代码如下: <,,'utf-8'}..<else/>{sh:$vo.name}</if> 这里if中的函 ...

  2. thread_local变量

    thread_local变量是C++ 11新引入的一种存储类型.它会影响变量的存储周期(Storage duration),C++中有4种存储周期: automatic static dynamic ...

  3. 使用QuartZ.net来做定时计划任务 ; 值不能为 null。 参数名: assemblyString

    1. 当异常的时候, 发现需要的类名, 没有取到, 然后就发生异常了 2. 分析: 业务层调用数据层, 数据层去掉配置的时候, 发现配置文件中根本就没有配置, 这个时候使用反射来取, 肯定是拿不到的, ...

  4. oracle显示数据库名和表名

    oracle查看表名 select table_name from user_tables; select table_name from dba_tables; select * from all_ ...

  5. python's fourteenth day for me 内置函数

    locals:  函数会以字典的类型返回当前位置的全部局部变量. globals:  函数会以字典的了类型返回全部的全局变量. a = def func(): b = print(locals()) ...

  6. 浅谈 Python 的 with 语句 【转载】

    引言 with 语句是从 Python 2.5 开始引入的一种与异常处理相关的功能(2.5 版本中要通过 from __future__ import with_statement 导入后才可以使用) ...

  7. Linux字符设备驱动实现

    Linux字符设备驱动实现 要求 编写一个字符设备驱动,并利用对字符设备的同步操作,设计实现一个聊天程序.可以有一个读,一个写进程共享该字符设备,进行聊天:也可以由多个读和多个写进程共享该字符设备,进 ...

  8. 说说API的重放机制

    API的重放机制 我们在设计接口的时候,最怕一个接口被用户截取用于重放攻击.重放攻击是什么呢?就是把你的请求原封不动地再发送一次,两次...n次,一般正常的请求都会通过验证进入到正常逻辑中,如果这个正 ...

  9. Codeforces 1120D (树形DP 或 最小生成树)

    题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...

  10. SpringBoot23 分模块开发

    1 开发环境说明 JDK:1.8 MAVEN:3.5 IDEA:2017.2.5 SpringBoot:2.0.3.RELEASE 2 创建SpringBoot项目 2.1 项目信息 2.2 添加项目 ...