【问题标题】:How to add different icons/images to datagridview row header in c#?如何在 c# 中向 datagridview 行标题添加不同的图标/图像?
【发布时间】:2011-07-07 16:30:06
【问题描述】:

我想动态地将不同的图像添加到 c# windows 表单 datagridview 行标题。它应该像检查任何单元格值一样,如果它>10 显示一些图像,否则显示其他图像。如何做到这一点?请帮助我............

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    您可以在 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.
        }
    }
    

    【讨论】:

      【解决方案2】:

      向 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

      【讨论】:

      • 将图像添加到 headertemplate 并在 onrowdatabound 事件中隐藏它或使用 url 分配它。
      【解决方案3】:

      我不完全确定如何添加图像...但是this link 有一个很好的例子,可以在行标题中写入数字,因此您可以使用 >10 条件对其进行更新,以显示您的图像。 .

      【讨论】:

      • 我不想在行标题中添加数字。我想在不同的场合添加不同的图像添加行标题。它检查读取的单元格内容。你能帮我......
      • 抱歉,我错过了单元格值检查,我不确定您是否可以通过此活动做到这一点。对不起!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多