Solved

Get cell value of gridview on command argument

Posted on 2011-02-17
7
1,419 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:kdeutsch
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
Kaushal Arora earned 250 total points
ID: 34917550
To use the CommandArgument as the index, you have to assign the index in the Griview in the Design of the gridview.

In the button with the CommandName = "YourCommand" give
CommandArgument = '<% # Container.DataItemIndex %>'

Also one thing more you are using the CommandName as 'Update' for this command you have to handle the Gridview_RowUpdating Event and there you can find the index as e.Row.RowIndex.

Hope it helps.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34917602
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


0
 

Author Comment

by:kdeutsch
ID: 34917842
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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Expert Comment

by:Kaushal Arora
ID: 34918161
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.
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 150 total points
ID: 34918458
One way as mentioned by KaushalArora  with Update you use OnRowUpdating event and you can get the old and new values from the event arguments to get the values you want.

Or what you can do is instead of using then CommantName="Update", you use your own name like "MyUpdate" and then you can use RowCommand + the earlier suggestion  but check for MyCommand. Also for the radiobutton you will have do to a findcontrol inside of your gridview like myGridview.Rows(index).Cells(3).FindControl("yourradiobuttonid")
0
 

Author Comment

by:kdeutsch
ID: 34926021
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
0
 

Author Closing Comment

by:kdeutsch
ID: 34926184
Ok, finally got what I need by googling more on web about update row for gridview.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question