[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1767
  • Last Modified:

Obtaining Gridview Column Data within a RowCommand Event

Hi

I have a Gridview Control which uses the built in Delete command.

I would like to use this Delete command to to not only delete a DB record so I am using the GridView1_RowCommand event like so:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Delete" Then
            Do some stuff
        End If
    End Sub

What I need to be able to do within this event is obtain the Record ID from the currently selected record that I am performing the Delete on from the Gridwiew which is situated within the first column (column 0).

Please anyone suggest how to acheive this?

Many thanks

Rit
0
rito1
Asked:
rito1
  • 4
  • 4
1 Solution
 
Bob LearnedCommented:
Try something like this:

Dim drv As DataRowView = CType(e.Row.RowItem, DataRowView)
Dim recordID As Integer = CInt(drv("RecordID").ToString())

Bob
0
 
rito1Author Commented:
thanks Bob,

Here is my code so far but the error I get is Type 'DataRowView' is not defined:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Delete" Then
            Dim drv As DataRowView = CType(e.Row.RowItem, DataRowView)
            Dim recordID As Integer = CInt(drv("RecordID").ToString())

            Label1.Text = recordID
        End If
    End Sub
0
 
Bob LearnedCommented:
You need a Imports for System.Data for the page.

Bob
0
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!

 
rito1Author Commented:
Hi Bob

Sorry, I now seem to get an error on e.Row:

BC30390: 'System.Web.UI.WebControls.GridViewCommandEventArgs.Private ReadOnly Property Row() As System.Web.UI.WebControls.GridViewRow' is not accessible in this context because it is 'Private'.
0
 
Bob LearnedCommented:
Oops, I was trying from memory, and that was for RowDataBound and not RowCommand:

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim index As Integer = CInt(e.CommandArgument)
        Dim row As GridViewRow = Me.GridView1.Rows(index)
        Dim drv As DataRowView = CType(row.DataItem, DataRowView)
        Dim recordID As Integer = CInt(drv("RecordID").ToString())
    End Sub

Bob

0
 
rito1Author Commented:
Hi Bob

To be able to whip that off from the top of your head show you are the Genius status :-)

I am still getting an error on the following line:

Dim recordID As Integer = CInt(drv("ID").ToString())

Err:
System.NullReferenceException: Object reference not set to an instance of an object.

I have even change the delete button over to a template column and renamed the command which still throws error...

Dim index As Integer = CInt(e.CommandArgument)

System.FormatException: Input string was not in a correct format.
0
 
Bob LearnedCommented:
1) The DataSource must be set, even after a post-back for DataItem to work.

2) If you look at the MSDN RowCommand help, you will see what I was talking about:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

3) If you have a Delete button, then it will fire the RowDeleting and RowDeleted events.  You can get the RowIndex from the GridViewDeleteEventArgs event arguments.

Bob
0
 
rito1Author Commented:
Thanks  for your help Bob,

I think I need to do a little research on this before I move forward.

Rit
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now