Link to home
Start Free TrialLog in
Avatar of rp
rpFlag for Portugal

asked on

asp.net vb.net gridview name cells

It´s possible in the following example use the name of column instead the column number:

row.Cells(2).Text

for ex: like:

row.Cells("name").Text
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

yes you can do this.  In fact it's preferred because if you change your columns around programattically your indexes will all get messed up.  
Avatar of rp

ASKER

But when i try with this code i get an error message in msgbox() line :


    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        If e.CommandName = "Add" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index)
            MsgBox(row.Cells("name").Text)
        End If

    End Sub


Error message:       
The conversion of the chain "name" to type 'Integer' is not valid.
it should just be row("name") . . .  not cells.
Avatar of rp

ASKER

in this case not work, is expected an integer as index
row('name") is for a datareader . . . sorry.

should be row.Item("Name")
Avatar of rp

ASKER

when i try use your code, the help context show the following message:

'item' is not a member of System.Web.UI.WebControls.Gridviewrow
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rp

ASKER

how should do