DataGridView 自定义行样式和行标题

转自:http://blog.163.com/cw490@126/blog/static/365940742010611113450284/ 

示例代码:http://download.csdn.net/source/791182

本示例演示根据数据内容设置DataGridView控件的各种样式,包括行样式、单元格样式、行标题内容以及行标题图标。在DataGridView控件的CellFormattingRowPostPaint事件中进行相关的操作即可实现。

这里假设数据是学生成 绩单,包含班级、姓名和成绩三个字段。

实现效果为:

1、相邻行的班级信息相同则用相同的行样式,用两种样式交替显示

2、相邻行的班级信息如果相同则只显示一次,选中时显示必须班级信息

2、成绩不及格(<60)的用特殊样式显示

3、在标题上显示行号

4、在行标题上根据成绩等级显示不同的图标以及不同的提示信息

 

下面介绍具体实现过 程。

在界面上放置一个DataGridView控件,添加数据行并设置相关属性。

添加图片到资源文件 中。

 

定义两个样式对象:

  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""89" });
  12.             m_GradeTable.Rows.Add(new string[] { "Class1""Jack""77" });
  13.             m_GradeTable.Rows.Add(new string[] { "Class1""Bill""91" });
  14.             m_GradeTable.Rows.Add(new string[] { "Class2""Tom""58" });
  15.             m_GradeTable.Rows.Add(new string[] { "Class2""Rose""95" });
  16.             m_GradeTable.Rows.Add(new string[] { "Class3""Peter""64" });
  17.             m_GradeTable.Rows.Add(new string[] { "Class3""David""82" });
  18.             m_GradeTable.Rows.Add(new string[] { "Class3""Eric""68" });
  19.             m_GradeTable.Rows.Add(new string[] { "Class3""Lily""79" });
  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 + 1);//显示行号,也可以设置成显示其他信 息
  9.                 //CurrentRow.HeaderCell.ToolTipText = "当前 第" + Convert.ToString(e.RowIndex + 1) + "行";//设置ToolTip信息

  10.                 //以下为根据上一行内容判断所属组的效果
  11.                 if (e.RowIndex == 0)//首行必须特殊处理,将其设置为常规 样式
  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 - 1].Cells[e.ColumnIndex].Value.ToString())
  22.                     {
  23.                         CurrentRow.DefaultCellStyle = this.dgvGrade.Rows[e.RowIndex - 1].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 - 1].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) < 60)//对不及格的成绩设置特殊样式
  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 >= 90)
  10.             {
  11.                 RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeA;//从资源文件中获取图片
  12.                 strToolTip = "Grade A";
  13.             }
  14.             else if (intGrade >= 80)
  15.             {
  16.                 RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeB;
  17.                 strToolTip = "Grade B";
  18.             }
  19.             else if (intGrade >= 70)
  20.             {
  21.                 RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeC;
  22.                 strToolTip = "Grade C";
  23.             }
  24.             else if (intGrade >= 60)
  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 - 20, e.RowBounds.Top + 4, 16, 16);//绘制图标
  35.             this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText = strToolTip;//设置提示信息

  36.         }

 

 

代码中注释比较详细,具体的细节就不逐步介绍了。如果需要绘制行标题图标,最好把DataGridView控件的AllowUserToResizeRows属性设置为False,把RowHeadersWidthSizeMode设置为DisableResizing,然后自己设置RowHeadersWidth的值。也可以根据绘制多个图标,在我的小程序DataBaseScript中就有应用,效果如下:


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值