vb.net Gridview - 基于列数据的颜色单元格行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13376955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:07:54  来源:igfitidea点击:

Gridview - colour cell row based on column data

asp.netvb.netgridviewboundfield

提问by Charlie

Using VWD 2010 Express, ASP.NET, VB.NET.

使用 VWD 2010 Express、ASP.NET、VB.NET。

I have a gridview that is bound to a database table, it contains 10 columns, one of which is hidden.

我有一个绑定到数据库表的 gridview,它包含 10 列,其中一列是隐藏的。

I would like to set a background colour of the rows which have a due date (the last column, index 8 in gridview) of anything before the current day.

我想设置在当天之前有任何截止日期(最后一列,gridview 中的索引 8)的行的背景颜色。

So far I have this: (defined under the RowCreated event)

到目前为止,我有这个:(在 RowCreated 事件下定义)

Dim i As Integer = 8
If e.Row.RowType = DataControlRowType.DataRow Then
    If e.Row.Cells(i).Text <= Date.Now.ToString Then
        e.Row.Cells(i).ForeColor = System.Drawing.Color.Red
    End If
End If

However, problem, I receive an error:

但是,问题,我收到一个错误:

"Specified argument was out of the range of valid values, parameter: index"

“指定的参数超出了有效值的范围,参数:索引”

All the objects in the gridview are boundfields. The only index which doesn't return this error is index 0, any advice?

gridview 中的所有对象都是边界字段。唯一不返回此错误的索引是索引 0,有什么建议吗?

When using index 0, it only colours the very first cell in each of the rows, and I don't think that's dependant on the date at all, as it seems to colour them all red.

使用索引 0 时,它只为每一行中的第一个单元格着色,我认为这根本不依赖于日期,因为它似乎将它们全部着色为红色。

回答by Charlie

Managed to find a fix, instead of using the index of the column, I used the column name, like so:

设法找到了一个修复程序,而不是使用列的索引,我使用了列名,如下所示:

    If e.Row.DataItem("DueDate") < Date.Now.ToString Then
        e.Row.BackColor = Drawing.Color.Red
    End If

NOTE: I declared this in RowDataBound, not in RowCreated.

注意:我在 RowDataBound 中声明了这一点,而不是在 RowCreated 中。

Works perfectly, left this here for anyone else with a similar problem. Indexing doesn't seem very accurate in this instance.

完美运行,把这个留在这里给其他有类似问题的人。在这种情况下,索引似乎不太准确。