【发布时间】:2011-07-07 16:30:06
【问题描述】:
我想动态地将不同的图像添加到 c# windows 表单 datagridview 行标题。它应该像检查任何单元格值一样,如果它>10 显示一些图像,否则显示其他图像。如何做到这一点?请帮助我............
【问题讨论】:
标签: c# winforms datagridview
我想动态地将不同的图像添加到 c# windows 表单 datagridview 行标题。它应该像检查任何单元格值一样,如果它>10 显示一些图像,否则显示其他图像。如何做到这一点?请帮助我............
【问题讨论】:
标签: c# winforms datagridview
您可以在 DataGridView.RowPostPaint 事件中将图像添加到 DataGridView 行标题。
这是 CodeProject 上一篇文章的链接,该文章似乎很好地描述了这一点(我没有尝试自己编写代码):Row Header Cell Images in DataGridView
您可以使用 RowPostPaint 事件提取要测试的值以确定要显示的图标。通过使用事件的 RowIndex 属性和您感兴趣的列的索引值来执行此操作。
这样的事情应该作为一个起点:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) {
// As an example we'll check contents of column index 1 in our DGV.
string numberString = dataGridView1.Rows[e.RowIndex].Cells[1].Value as string;
if (numberString != null) {
int number;
if (Int32.TryParse(numberString, out number)) {
if (number > 10) {
// Display one icon.
} else {
// Display the other icon.
}
} else {
// Do something because the string that is in the cell cannot be converted to an int.
}
} else {
// Do something because the cell Value cannot be converted to a string.
}
}
【讨论】:
向 GridView 添加 OnRowDataBound 事件处理程序
在事件处理程序中 - 检查标题并相应地处理每一列
protected virtual void OnRowDataBound(GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
// process your header here..
}
}
欲了解更多信息,请访问:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewroweventargs.row.aspx
【讨论】:
我不完全确定如何添加图像...但是this link 有一个很好的例子,可以在行标题中写入数字,因此您可以使用 >10 条件对其进行更新,以显示您的图像。 .
【讨论】: