×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Get value of datagrid row with button click

Get value of datagrid row with button click

Get value of datagrid row with button click

(OP)
I have an app in asp 2.0.

I have a datagrid with three columns and three buttons on a row.

I want to click one of the buttons and get the value of the first column.

How do I do that? I know to use the GridView1_RowCommand event to get the buttons to do things, but I don't know how to get the value of a cell from the click of a button. Is clicking the button the same as selecting the row?

Thanks in advance for whatever help you can give.

ProDev, MS Access Applications
Visit me at ==> http://www.prodev.us

May God bless you beyond your imagination!!!

RE: Get value of datagrid row with button click

You're on the right track, but you need to use 2 events for this.
First the RowDataBoundEvent

CODE

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
'The DataKeys property can be used if you want the ID of
'the table, for example the Primay Key of the table.
'If you want a particular cell other than the ID of the table, use the lines I have commented out below.
   If e.Row.RowType = DataControlRowType.DataRow Then
      Dim btnOne, btnTwo, btnThree As New Button
      btnOne = e.Row.FindControl("btnOne")
      btnOne.CommandArgument = GridView1.DataKeys(e.Row.RowIndex).Value.ToString
       'For a particular cell ...
       'btnOne.CommandArgument = e.Row.Cells(cell index).Text  

       btnTwo = e.Row.FindControl("btnTwo")
       btnTwo.CommandArgument = GridView1.DataKeys(e.Row.RowIndex).Value.ToString
       'For a particular cell ...
       'btnTwo.CommandArgument = e.Row.Cells(cell index).Text  


       btnThree = e.Row.FindControl("btnThree")
       btnThree.CommandArgument = GridView1.DataKeys(e.Row.RowIndex).Value.ToString
       'For a particular cell ...
       'btnThree.CommandArgument = e.Row.Cells(cell index).Text  

   End If

End Sub
Now the RowCommand event:

CODE

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

            Case "One"
                Response.Write("One" + "<BR>")
                Response.Write(e.CommandArgument + "<BR>")

            Case "Two"
                Response.Write("Two" + "<BR>")
                Response.Write(e.CommandArgument + "<BR>")

            Case "Three"
                Response.Write("Three" + "<BR>")
                Response.Write(e.CommandArgument + "<BR>")

        End Select

Hope this helps you...

Jim

RE: Get value of datagrid row with button click

Did you ever get this resolved?

Jim

RE: Get value of datagrid row with button click

(OP)
Yes actually. I took what you had above and played around until I came up with this...

CODE

        Select Case e.CommandName

            Case "AddLevelIntro"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Dim row As GridViewRow = Me.GridView1.Rows(index)

                MsgBox(row.Cells(0).Text)
                MsgBox(row.Cells(1).Text)

        End Select

This was a case where someone was giving me something to help me learn while I was stuck on my actual project. Thanks. Sorry I didn't reply. I have been stuck on sending a crystal report straight to a user default printer from the server.

I have a post out there.

Thanks again.

ProDev, MS Access Applications
Visit me at ==> http://www.prodev.us

May God bless you beyond your imagination!!!

RE: Get value of datagrid row with button click

ok..glad you got it working

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close