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. GridViewCo mmandEvent Args) 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
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
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
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. GridViewCo mmandEvent Args) Handles GridView1.RowCommand
If e.CommandName = "Delete" Then
Dim drv As DataRowView = CType(e.Row.RowItem, DataRowView)
Dim recordID As Integer = CInt(drv("RecordID").ToStr ing())
Label1.Text = recordID
End If
End Sub
Here is my code so far but the error I get is Type 'DataRowView' is not defined:
Protected Sub GridView1_RowCommand(ByVal
If e.CommandName = "Delete" Then
Dim drv As DataRowView = CType(e.Row.RowItem, DataRowView)
Dim recordID As Integer = CInt(drv("RecordID").ToStr
Label1.Text = recordID
End If
End Sub
You need a Imports for System.Data for the page.
Bob
Bob
ASKER
Hi Bob
Sorry, I now seem to get an error on e.Row:
BC30390: 'System.Web.UI.WebControls .GridViewC ommandEven tArgs.Priv ate ReadOnly Property Row() As System.Web.UI.WebControls. GridViewRo w' is not accessible in this context because it is 'Private'.
Sorry, I now seem to get an error on e.Row:
BC30390: 'System.Web.UI.WebControls
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. GridViewCo mmandEvent Args) 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").ToStr ing())
End Sub
Bob
Protected Sub GridView1_RowCommand(ByVal
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").ToStr
End Sub
Bob
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.NullReferenceExcept ion: 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.
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.NullReferenceExcept
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help Bob,
I think I need to do a little research on this before I move forward.
Rit
I think I need to do a little research on this before I move forward.
Rit
Dim drv As DataRowView = CType(e.Row.RowItem, DataRowView)
Dim recordID As Integer = CInt(drv("RecordID").ToStr
Bob