Link to home
Start Free TrialLog in
Avatar of kdeutsch
kdeutschFlag for United States of America

asked on

Get cell value of gridview on command argument

Seems like I run into this everytime in a different situation trying to get gridview values and its a different syntax everytime.  If anyone has a greate site that goes through all the different ways to get at the values please share.  trying to get the values on a Row Command agrument,
I have text fields and a radio button that seems none of these work to get the values.


If (e.CommandName = "Update") Then
            Dim id As Integer = myGridview.DataKeys(e.CommandArgument).Value
            'Dim data As TextBox = TryCast(myGridview.FindControl("txtData"), TextBox)
            Dim data As String = myGridview.Rows(0).Cells(3).Text
            Dim Remarks As String = myGridview.Rows(e.CommandArgument).Cells(4).Text
            'Dim Remarks As TextBox = TryCast(myGridview.FindControl("txtRemarks"), TextBox)
            Dim DtDate As String = myGridview.Rows(e.CommandArgument).Cells(5).Text
            'Dim Dtdate As TextBox = TryCast(myGridview.FindControl("txtCal"), TextBox)
            'Dim answer As RadioButtonList = TryCast(myGridview.FindControl("rblAll"), RadioButtonList)

            sql = "Update tblSrpEventData set strData = '" & data & "', strRemarks = '" & Remarks & "', dtDoc = '" & DtDate & "', " _
                & "dtlogged = '" & Date.Now & "' where intDataId = " & id
            Response.Write(sql)
            Response.End()
            insertUpdateDelete(sql)

        End If
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kaushal Arora
Kaushal Arora
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What are you passing in CommandArgument??

One simple way is to pass the rowIndex as the CommandArgument like:
CommandArgument='<%# Container.DataItemIndex %>'

Then you get the rowindex and its simple to get cell values like:

 Dim index As Integer =Convert.ToInt32(e.CommandArgument)
 Dim id As Integer = myGridview.DataKeys(index).Value

 Dim data As String = myGridview.Rows(index).Cells(3).Text


Avatar of kdeutsch

ASKER

All,

I am simply trying to do an Update to a sql table when they click on a link button on the gridview it passes a commandname.

From here I want to get the values of the following cells
Rediobutton in cell 3
textbox in cell 4
textbox in cell 5
textbox in cell 6

Protected Sub myGridview_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles myGridview.RowCommand

        If (e.CommandName = "Update") Then
            Dim id As Integer = myGridview.DataKeys(e.CommandArgument).Value

                     sql = "Update tblSrpEventData set strData = '" & data & "', strRemarks = '" & Remarks & "', dtDoc = '" & DtDate & "', " _
                & "dtlogged = '" & Date.Now & "' where intDataId = " & id
            Response.Write(sql)
            Response.End()
            insertUpdateDelete(sql)

        End If
    End Sub
If you have given the CommandName as 'Update' then you have to handle the RowUpdating event for handling this. If the CommandName is 'Update' then it would not come to the RowCommand Event.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All,
Ok I changed to the rowUpdating event and put the following into it, but I am getting no data when i do  a respoonse.write.

 Protected Sub myGridview_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles myGridview.RowUpdating
        Dim UserLogon As String = Split(Current.User.Identity.Name, "\")(1)
        Dim Id As Integer = myGridview.DataKeys(e.RowIndex).Value
        Dim data As String = myGridview.Rows(e.RowIndex).Cells(3).Text
        Dim Remarks As String = myGridview.Rows(e.RowIndex).Cells(4).Text
        Dim DtDoc As String = myGridview.Rows(e.RowIndex).Cells(5).Text
        Dim answer As RadioButtonList = myGridview.Rows(e.RowIndex).FindControl("rblAll")

        sql = "Update tblSrpEventData set bitAnswer = " & answer.SelectedValue & ", strData = '" & data & "', strRemarks = '" & Remarks & "', dtDoc = '" & DtDoc & "', strLogged = '" & UserLogon & "', " _
            & "dtlogged = '" & Date.Now & "' where intDataId = " & Id

        Response.Write(sql)
        Response.End()
        insertUpdateDelete(sql)
    End Sub
Ok, finally got what I need by googling more on web about update row for gridview.