Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get value of datagrid row with button click 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Joined
Apr 16, 2001
Messages
2,628
Location
US
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 ==>
May God bless you beyond your imagination!!!
 
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
 
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 ==>
May God bless you beyond your imagination!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top