tollamie
tollamie

Reputation: 117

Get column value from a gridview using VB.net

I have in my GridView the Column(Validate), this column in contains two values 'v' or 'notv' I want to get the value 'v' from myGridview and change it to 'VALIDATE' and the other 'notv' to 'Not validate'.. I have Tried GridView1.Rows(i).Cells(10).Text But nothing has been changed when my Gridview is displayed.

Here is my code:

The GridView is displyed whan I click on Button:

<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

Thank you for your Time!

Upvotes: 1

Views: 38540

Answers (3)

tollamie
tollamie

Reputation: 117

It works for me just Like that without using the RowDataBound:

Here is the code behind:

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

Thank you all for your answer!

Upvotes: 1

Md. Parvez Alam
Md. Parvez Alam

Reputation: 4596

firstly this should be on rowdatabound event

and then you can try with trimming the spaces, could be it creating problem

If GridView1.Rows(i).Cells(10).Text = "v" Then
                GridView1.Rows(i).Cells(10).Text = "VALIDATE"

change to

If GridView1.Rows(i).Cells(10).Text.Trim() = "v" Then
                GridView1.Rows(i).Cells(10).Text = "VALIDATE"

also check for the header row in rowdatabound event

if gridview is having bound filed then it should work, if you are using templates, then you should use find control to find the specific control and then change its value

Upvotes: 3

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

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.

Upvotes: 0

Related Questions