access the data of Gridview in Rowcommand

I am developing several ASP.NET 2.0 application to get expirence from real world problems. Today i found a simple issue realting Gridviews commands. I have a buttonfield in gridview. The attribut commandname is used to make the logic decesion

<asp:ButtonField CommandName="profil" HeaderText="Profil" ShowHeader="True" Text="mail" />

The corresponding event is RowCommand

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

Select Case e.CommandName

Case "profil"

...

Now the question: how to access the values of a special cell?

You have two options. The datakeys or to adress a cell. But you need the actual row. This is done by the following line of code

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

Then get the key or get the cell and cast to a datacontrolfield.

Dim id As Guid = GridView1.DataKeys(row.RowIndex).Value

Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text

Remark: this only works with Boundfields.

[UPDATE:]

This doenst work with final bits. Read http://weblogs.asp.net/hpreishuber/admin/EditPosts.aspx

30 Comments

Comments have been disabled for this content.