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
rflorencioAsked:
Who is Participating?
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
row needs to be a DataRow.  you may need to cast it using Ctype
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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.  
0
 
rflorencioAuthor Commented:
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.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Kyle AbrahamsSenior .Net DeveloperCommented:
it should just be row("name") . . .  not cells.
0
 
rflorencioAuthor Commented:
in this case not work, is expected an integer as index
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
row('name") is for a datareader . . . sorry.

should be row.Item("Name")
0
 
rflorencioAuthor Commented:
when i try use your code, the help context show the following message:

'item' is not a member of System.Web.UI.WebControls.Gridviewrow
0
 
rflorencioAuthor Commented:
how should do
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.