简体   繁体   English

如何在 c# 中的 datagridview 行 header 中添加不同的图标/图像?

[英]How to add different icons/images to datagridview row header in c#?

I want to add different image to c# windows form datagridview row header dynamically.我想动态地将不同的图像添加到 c# windows 表单 datagridview 行 header 。 it should be do like check any cell value and if it>10 display some image,else display other image.How to do this one?please help me...........它应该像检查任何单元格值一样,如果它>10 显示一些图像,否则显示其他图像。如何做到这一点?请帮助我............

Add a OnRowDataBound event handler to the GridView将 OnRowDataBound 事件处理程序添加到 GridView

In the event handler - check for the header and process each column accordingly在事件处理程序中 - 检查 header 并相应地处理每一列

protected virtual void OnRowDataBound(GridViewRowEventArgs e) {
   if (e.Row.RowType == DataControlRowType.Header) {
       // process your header here..
    }
}

For more info go here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewroweventargs.row.aspx有关更多信息 go 此处: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridrowview。

You can add images to DataGridView row headers in the DataGridView.RowPostPaint event.您可以在DataGridView.RowPostPaint事件中将图像添加到 DataGridView 行标题。

Here's a link to an article on CodeProject that appears to describe this fairly well (I haven't tried to code myself): Row Header Cell Images in DataGridView这是 CodeProject 上一篇文章的链接,该文章似乎很好地描述了这一点(我没有尝试自己编写代码): Row Header Cell Images in DataGridView

You can use the RowPostPaint event to extract the value you want to test against to determine which icon you want to display.您可以使用 RowPostPaint 事件提取要测试的值以确定要显示的图标。 Do this by using the event's RowIndex property with the index value of the column you're interested in.通过使用事件的 RowIndex 属性和您感兴趣的列的索引值来执行此操作。

Something like this should serve as a starting point:像这样的东西应该作为一个起点:

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.
    }
}

I'm not exactly sure how to add images... but this link has a good example of writing numbers in the row headers, so you could update it with the >10 condition, to display your image.我不完全确定如何添加图像......但这个链接有一个很好的例子,在行标题中写入数字,所以你可以用 >10 条件更新它,以显示你的图像。 . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM