打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DataGridView自定义行样式和行标题
示例代码:http://download.csdn.net/source/791182
本示例演示根据数据内容设置DataGridView控件的各种样式,包括行样式、单元格样式、行标题内容以及行标题图标。在DataGridView控件的CellFormatting和RowPostPaint事件中进行相关的操作即可实现。
这里假设数据是学生成绩单,包含班级、姓名和成绩三个字段。
实现效果为:
1、相邻行的班级信息相同则用相同的行样式,用两种样式交替显示
2、相邻行的班级信息如果相同则只显示一次,选中时显示必须班级信息
2、成绩不及格(<60)的用特殊样式显示
3、在标题上显示行号
4、在行标题上根据成绩等级显示不同的图标以及不同的提示信息
下面介绍具体实现过程。
在界面上放置一个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", "89" });
m_GradeTable.Rows.Add(new string[] { "Class1", "Jack", "77" });
m_GradeTable.Rows.Add(new string[] { "Class1", "Bill", "91" });
m_GradeTable.Rows.Add(new string[] { "Class2", "Tom", "58" });
m_GradeTable.Rows.Add(new string[] { "Class2", "Rose", "95" });
m_GradeTable.Rows.Add(new string[] { "Class3", "Peter", "64" });
m_GradeTable.Rows.Add(new string[] { "Class3", "David", "82" });
m_GradeTable.Rows.Add(new string[] { "Class3", "Eric", "68" });
m_GradeTable.Rows.Add(new string[] { "Class3", "Lily", "79" });
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 + 1);//显示行号,也可以设置成显示其他信息
//CurrentRow.HeaderCell.ToolTipText = "当前第" + Convert.ToString(e.RowIndex + 1) + "行";//设置ToolTip信息
//以下为根据上一行内容判断所属组的效果
if (e.RowIndex == 0)//首行必须特殊处理,将其设置为常规样式
{
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 - 1].Cells[e.ColumnIndex].Value.ToString())
{
CurrentRow.DefaultCellStyle = this.dgvGrade.Rows[e.RowIndex - 1].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 - 1].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) < 60)//对不及格的成绩设置特殊样式
{
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 >= 90)
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeA;//从资源文件中获取图片
strToolTip = "Grade A";
}
else if (intGrade >= 80)
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeB;
strToolTip = "Grade B";
}
else if (intGrade >= 70)
{
RowIcon = TestDataGridViewRowStyle.Properties.Resources.GradeC;
strToolTip = "Grade C";
}
else if (intGrade >= 60)
{
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 - 20, e.RowBounds.Top + 4, 16, 16);//绘制图标
this.dgvGrade.Rows[e.RowIndex].HeaderCell.ToolTipText = strToolTip;//设置提示信息
}
代码中注释比较详细,具体的细节就不逐步介绍了。如果需要绘制行标题图标,最好把DataGridView控件的AllowUserToResizeRows属性设置为False,把RowHeadersWidthSizeMode设置为DisableResizing,然后自己设置RowHeadersWidth的值。也可以根据绘制多个图标,在我的小程序DataBaseScript中就有应用,效果如下:
原文:http://blog.csdn.net/ConExpress/article/details/3338481
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c# – 在编辑模式下更改datagridview单元格值
C#实例:datagridview单元格合并
Word VBA技术:判断表格中的单元格是否为空
C#winform DataGridView 右键选中正行
C#把DataSet转换成stirng进行传输
Windows Forms DataGridView 中合并单元格(转)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服