我在GridView中有列(验证),此列包含两个值'v'或'notv'我想从myGridview获取值' v '并将其更改为' 有效'和其他' notv '到'未验证'..我已经尝试GridView1.Rows(i).Cells(10).Text
但是没有任何改变我的Gridview会显示出来。
这是我的代码:
当我点击Button:
时,GridView显示出来<asp:GridView ID="GridView1" runat="server" CssClass="table" GridLines="None">
</asp:GridView>
“page.aspx.vb
cmd.Connection = cx
cmd.CommandText = "select .., validate from table"
da = New SqlDataAdapter(cmd)
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
For i = 0 To GridView1.Rows.Count - 1
GridView1.Rows(i).Cells(0).ForeColor = System.Drawing.Color.Blue
If GridView1.Rows(i).Cells(10).Text = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Green
ElseIf GridView1.Rows(i).Cells(10).Text = "Notv" Then
GridView1.Rows(i).Cells(10).Text = "Not VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Red
End If
Next
感谢您的时间!
答案 0 :(得分:2)
首先应该在rowdatabound事件
上然后你可以尝试修剪空格,可能会造成问题
If GridView1.Rows(i).Cells(10).Text = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
更改为
If GridView1.Rows(i).Cells(10).Text.Trim() = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
还检查rowdatabound事件中的标题行
如果gridview有绑定字段然后它应该工作,如果你使用模板,那么你应该使用find控件来查找特定控件然后更改其值
答案 1 :(得分:1)
对我来说就像没有使用RowDataBound那样:
这是背后的代码:
For i = 0 To GridView1.Rows.Count - 1
GridView1.Rows(i).Cells(0).ForeColor = System.Drawing.Color.Blue
If GridView1.Rows(i).Cells(10).Text = "v" Then
GridView1.Rows(i).Cells(10).Text = "VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Green
ElseIf GridView1.Rows(i).Cells(10).Text = "Notv" Then
GridView1.Rows(i).Cells(10).Text = "Not VALIDATE"
GridView1.Rows(i).Cells(10).ForeColor = System.Drawing.Color.Red
End If
Next
谢谢大家的回答!
答案 2 :(得分:0)
尝试在GridView RowDataBound事件中进行更改,例如
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
'Do stuff with e.Row.Cells(0)
'Do stuff with e.Row.Cells(10)
'etc...
End Sub
它会为绑定到数据表的每一行触发,因此您不必循环遍历行。