Link to home
Start Free TrialLog in
Avatar of rito1
rito1

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Try something like this:

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

Bob
Avatar of rito1
rito1

ASKER

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
You need a Imports for System.Data for the page.

Bob
Avatar of rito1

ASKER

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'.
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

Avatar of rito1

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rito1

ASKER

Thanks  for your help Bob,

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

Rit