根据列值更改网格视图行的字体颜色。无法索引gridviewrow吗?
我在 drr(5) 处遇到语法错误,其中 5 是我想要基于颜色更改的列。 当我使用数据集时,此方法有效
Dim Land As String = "Land"
Dim Air As String = "Air"
Dim Cruise As String = "Cruise"
Dim y As String
For Each drr As gridviewrow In GridView2.Rows
y = drr(5).ToString()
If y = Land Then
e.Row.ForeColor = System.Drawing.Color.LightGreen
ElseIf y = Air Then
e.Row.ForeColor = System.Drawing.Color.Red
ElseIf y = Cruise Then
e.Row.ForeColor = System.Drawing.Color.Green
End If
Next
i am getting a syntax error at drr(5) which 5 is the column i want to base the color change on.
this method works when i am using a dataset
Dim Land As String = "Land"
Dim Air As String = "Air"
Dim Cruise As String = "Cruise"
Dim y As String
For Each drr As gridviewrow In GridView2.Rows
y = drr(5).ToString()
If y = Land Then
e.Row.ForeColor = System.Drawing.Color.LightGreen
ElseIf y = Air Then
e.Row.ForeColor = System.Drawing.Color.Red
ElseIf y = Cruise Then
e.Row.ForeColor = System.Drawing.Color.Green
End If
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
Text
变量,即单元格文本单词。use
Text
variable , that is the cell text word.当您想要访问网格视图行中的单元格时,应该使用该行的单元格属性。在您的示例中,您需要编写 drr.Cells(5).ToString()
正如
我还发现最好给该行一个类,然后使用 css 更改颜色。
When you want to access a cell in a grid view row, you should use the cell property of the row. In your example you need to write drr.Cells(5).ToString()
As in
Also I find that it's better to give the row a class and then change the color using css.
您应该根据这个值应用一个CssClass:
例如:
重要的部分是RowDataBound,如果您绑定GridView,它会自动调用。如果您不想将 CssClass 命名为与显示的文本完全相同的名称,则可以使用
If...Else
或Select Case
来设置 CSS 类。You should apply a CssClass according to this value:
for example:
The important part is in RowDataBound that is called automatically if you bind the GridView. If you don't want to name the CssClass exactly like the text that is displayed, you could use
If...Else
orSelect Case
to set the CSS-Class.关于更改 TableCells?这也行不通?
在 C# 中,但尝试一下。
And about changing the TableCells? This doen't work too?
In C#, but give a try.