1

我在我的 GridView 中有 Column(Validate),该列包含两个值“v”或“notv”我想从 myGridview 获取值“ v ”并将其更改为“ VALIDATE ”,另一个“ notv ”更改为“不验证'..我已经尝试GridView1.Rows(i).Cells(10).Text但是当我的 Gridview 显示时没有任何改变。

这是我的代码:

当我单击按钮时,会显示 GridView:

<asp:GridView ID="GridView1" runat="server" CssClass="table" GridLines="None">
                           </asp:GridView>

'页面.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

感谢您的时间!

4

3 回答 3

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 control来找到特定的控件,然后改变它的值

于 2013-08-21T04:59:48.390 回答
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

谢谢大家的回答!

于 2013-08-21T07:49:55.387 回答
0

Try making your changes in GridView's RowDataBound event e.g.

 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

It fires for every row bound to your datatable so you don't have to loop thru rows.

于 2013-08-21T04:49:41.783 回答