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

Published Friday, August 05, 2005 9:43 PM by preishuber

Comments

# re: access the data of Gridview in Rowcommand

Wednesday, August 23, 2006 10:45 PM by Hoong Foong

Thank you. This piece of short note helped me a lot.

# re: access the data of Gridview in Rowcommand

Wednesday, March 14, 2007 6:04 AM by s

thanks for this needed

# re: access the data of Gridview in Rowcommand

Wednesday, April 25, 2007 4:36 AM by Ben

More than one way to skin a cat I know; however I use

ctype(sender, GridView).DataKeys(e.CommandArgument)

Because e.CommandArgument is the row index that was clicked; and DataKeys is indexed so you can just look it up :)

Ben

# Re :: access the data of Gridview in Rowcommand

Wednesday, May 02, 2007 8:06 AM by Pintu Paraliya

Thank you very much !!!!!!

This is very useful for me

# re: access the data of Gridview in Rowcommand

Sunday, June 10, 2007 11:59 PM by nora

thanx so much,

this information is helpful and made my projects works fine :)

thank you again

# re: access the data of Gridview in Rowcommand

Tuesday, June 12, 2007 2:34 AM by Simran

access the data of Gridview  in Rowcommand using Template Field??

# re: access the data of Gridview in Rowcommand

Friday, June 29, 2007 7:14 AM by Nauman Altaf

Thanks a lot this will solve my problem...

# re: access the data of Gridview in Rowcommand

Thursday, August 09, 2007 4:37 AM by vishwa

thanks for the code, helped me lot,

# re: access the data of Gridview in Rowcommand

Tuesday, August 21, 2007 10:39 AM by Disturbed Buddha

I found it to be easier to add a Command Field (specifically a Select) rather than a button field.  You can change the text and make it look like a button if you so wish.  Then you can just use the gridview's SelectedIndexChanged event:

Protected Sub GridView1_SelectedIndexChanged ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

   Dim Row As GridViewRow = GridView1.Rows(GridView1.SelectedIndex)

   lblName.Text = Row.Cells(0).Text

   lblAddress.Text = Row.Cells(1).Text

   '...or whatever you want to do with the info.

End Sub

# re: access the data of Gridview in Rowcommand

Friday, September 28, 2007 1:41 PM by Luis Simoes

It doenst work with me... really! Using templateitems just returns me empty strings while trying to get labels values... Why is this? Any solution?

# re: access the data of Gridview in Rowcommand

Friday, November 09, 2007 3:08 PM by Babak Zawari

Thanks a lot this will solve my problem...

Babak Zawari

بابک زواري

# re: access the data of Gridview in Rowcommand

Wednesday, November 28, 2007 11:38 PM by HemaLatha

Dear All,

    I have one problem .In my gridview i want to update the status of one field by clicking linkbutton .For that i used one event i.e RowCommand .And also i have the property allowpaging=true but when i perform paging RowCommand will fired.In that Event i have written come code .

If you know the solution Please send me

# re: access the data of Gridview in Rowcommand

Saturday, December 29, 2007 12:12 PM by HISCommunications

HemaLatha,

Have you wrapped your code in the RowCommand event with a Select statement, If statement, or similar structure so that it only fires your code if your particular commandname is passed?

So you would have something like this:

Select Case e.CommandName

    Case "myfieldscommandname"

          'code to fire would go here

End Select

Doing this should mean that your code will only fire on your command, and not on all commands.

# re: access the data of Gridview in Rowcommand

Friday, January 25, 2008 5:36 AM by Prmaod

thanks you that was wonderful..

# re: access the data of Gridview in Rowcommand

Friday, January 25, 2008 2:44 PM by Mike

I have had success using Hannes code and Disturbed Buddha's suggestion. One problem I am encountering on both is, If I use the gridviews allowsorting where the rows can be sorted using the column headings, when clicking the Select button on a particular row after sorting, the key that is returned is the row key of the way the gridview looked with originally displayed.  So upon initial display, let's say row 2, cell 2 had a value of '6' and after sorting on one of the columns visually row 2 cell 2 now contains a '12', when I click the select command or button, row 2 cell 2 still holds a '6' rather than a '12'.

Has any one had this problem? Thanks

# re: access the data of Gridview in Rowcommand

Tuesday, January 29, 2008 12:46 AM by ullas krishnan

thankyoouu so muchh thankyoouuu

excellant examplee

thankyouu

it helps mee morree

thankyoouuuuuuuuuu

# re: access the data of Gridview in Rowcommand

Wednesday, March 05, 2008 6:42 PM by Viral

Thanks. This one line code solved my problem.

# re: access the data of Gridview in Rowcommand

Tuesday, March 11, 2008 5:40 AM by Rajesh

Thanks.Your article is very nice.

# re: access the data of Gridview in Rowcommand

Thursday, April 03, 2008 4:25 PM by ForceMagic

I have a problems with that. When I am editting the ButtonField.Text, the fields isn't clickable anymore !

<asp:ButtonField meta:resourcekey="Document" CommandName="ProjetStatusDetails"

HeaderText="Document(s)" ButtonType="Link">

</asp:ButtonField>

So this was my ButtonField. Now I assing a text to him during the RowDataBound Event.

gvProjetDocument as my GridView

"NbrDocument" as my Number of document for a specific Project (I perform a count in the SQL Query)

Protected Sub gvProjetDocument_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProjet.RowDataBound

       If e.Row.RowIndex >= 0 Then

           e.Row().Cells(ColonneName.Document).Text = GetLocalResourceObject("Document") & " (" & CType(gvProjet.DataSource, Data.DataTable).Rows(e.Row.RowIndex).Item("NbrDocument") & ")"

       End If

   End Sub

Like you see I don't change the text of the row -1 because it's the Header row title.

So everything works fine when I look at the Output I have something like : "Details (3)"

But this link isn't clikable anymore....

Anyone have an idea ?

# re: access the data of Gridview in Rowcommand

Thursday, April 03, 2008 4:52 PM by ForceMagic

Ok I found what was the problem.

When I was editting the .Text property, I think he replace all value in my <td></td> output for the text I placed there.

So I use that to solve the problem.

       If e.Row.RowIndex >= 0 Then

           CType(e.Row.Cells(ColonneName.Document).Controls.Item(0), LinkButton).Text = GetLocalResourceObject("Document") & " (" & CType(gvProjet.DataSource, Data.DataTable).Rows(e.Row.RowIndex).Item("NbrDocument") & ")"

       End If

# re: access the data of Gridview in Rowcommand

Thursday, April 10, 2008 8:49 AM by Camille

This comment is in response to using a template column.  On solution would be in the row command you could find the control by ID in the selected row.  Below is an example of code that would go in the rowcommand event.

Dim row As GridViewRow = CType(e.CommandSource, GridView).Rows(e.CommandArgument)

Dim ckbTemp As CheckBox = CType(row.FindControl("ckbCheckboxID"), CheckBox)

Dim lblTemp As Label = CType(row.FindControl("lblLabelID"), Label)

If e.CommandName = "EDIT" Then

lblLabelOnPage.Text = lblTemp.Text

End if

# re: access the data of Gridview in Rowcommand

Saturday, April 19, 2008 7:44 AM by Monalisa

Thank u very much This coding realy helps me alot in my project

# re: access the data of Gridview in Rowcommand

Tuesday, July 01, 2008 7:41 AM by Alex

When i tried to push any button momentally was showed error log:

Error in:

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

Error message:

Unable to cast object of type 'ASP.default_aspx' to type 'System.Web.UI.WebControls.GridViewRow'.

Can someone explain it for me?

Alex

# re: access the data of Gridview in Rowcommand

Tuesday, July 01, 2008 8:20 AM by Alex

Thanx for material I solved my problem. There are my code:

       Dim row As GridViewRow = CType(e.CommandSource, GridView).Rows(e.CommandArgument)

       Dim regnr As String = CType(row.Cells(1), DataControlFieldCell).Text

       lblTest.Text = regnr

# re: access the data of Gridview in Rowcommand

Thursday, July 10, 2008 3:34 AM by Pino

Great Tip!

Thanks a lot.

# access the data of Gridview in Rowcommand &laquo; Dot Net Learning Zone Blog

Pingback from  access the data of Gridview in Rowcommand  &laquo; Dot Net Learning Zone Blog

# re: access the data of Gridview in Rowcommand

Monday, August 25, 2008 10:10 AM by Deepthianns

Rhx for the material. It hepled me a lot.

# re: access the data of Gridview in Rowcommand

Friday, October 17, 2008 4:47 AM by YK

It's very helpful! Thanks! :)

# re: access the data of Gridview in Rowcommand

Saturday, December 06, 2008 5:53 AM by sudhir

This is not working when my gridvew is inside the ajax update panel and my aspx page is using a master page.

# re: access the data of Gridview in Rowcommand

Sunday, December 14, 2008 6:08 AM by darshan shah

Thanks a lot for this solution..........

it a great solution to deal with row command event of gridview.

# re: access the data of Gridview in Rowcommand

Thursday, February 19, 2009 9:39 AM by nish

it's vary helpful to me !!!!! thanks!!

# re: access the data of Gridview in Rowcommand

Sunday, August 30, 2009 7:32 AM by Akhtar Jahan

Thanks, It helps me a lot.

# re: access the data of Gridview in Rowcommand

Monday, August 31, 2009 7:40 AM by biswaranjan pattanaik

Please give a solution regarding this problem.

# re: access the data of Gridview in Rowcommand

Monday, March 07, 2011 8:13 AM by ashok_walke

Thank You

# re: access the data of Gridview in Rowcommand

Friday, March 18, 2011 5:22 AM by csram

I make it as my bookmark :)

Leave a Comment

(required) 
(required) 
(optional)
(required)