Solved

Get cell value of gridview on command argument

Posted on 2011-02-17
7
1,442 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 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 37
Error running webpage local computer 7 26
How to repeat the data 4 30
.NET - find out if application is running on Windows 2012 server 3 31
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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