?
Solved

Get cell value of gridview on command argument

Posted on 2011-02-17
7
Medium Priority
?
1,494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
Kaushal Arora earned 1000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 600 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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