Solved

Get cell value of gridview on command argument

Posted on 2011-02-17
7
1,404 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now