Getting a DataRow from an ASP.NET GridView?

Int index = Convert. ToInt32(e. CommandArgument).

Up vote 1 down vote favorite share g+ share fb share tw.

I have an ASP. NET GridView that's bound to an ObjectDataSource (which is bound to a MySQL database). On this grid, I have 2 unbound ButtonField columns that I want to trigger server-side events.

Hence I have added an eventhandler method to the GridView's RowCommand event. In the code of said eventhandler, I need to somehow get hold of the underlying DataRow that was clicked on by the user. However, I can't seem to get this to work; if I use code like the following the selectedRow variable is always null: protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e) { CallDataRow selectedRow = (CallDataRow) searchResultsGridView.RowsConvert.

ToInt32(e. CommandArgument). DataItem; } I've Googled and found pages such as http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event, but nothing I've found has worked.

I'm pretty sure I'm just missing something obvious, but I can't figure out what... Here's the ASP. NET code if that helps: c# asp.net gridview data event-handling link|improve this question asked Aug 20 '09 at 15:31Ian Kemp3,492927 76% accept rate.

Int index = Convert. ToInt32(e. CommandArgument); GridViewRow row = searchResultsGridView.Rowsindex.

That gives me a GridViewRow with RowType == DataRow and DataItem == null. Since it's the DataItem that I require to get hold of the underlying DataRow, and the DataItem is null in this case, this isn't an answer. – Ian Kemp Aug 21 '09 at 11:35.

Finally got it to work by doing the following: Adding a TemplateField containing a bound HiddenField. ' /> Adding the following code in the RowCommand event handler: protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e) { string audioFile = ((HiddenField) searchResultsGridView.RowsConvert. ToInt32(e.

CommandArgument). FindControl("audioFileName")). Value; } It's a cludge and it's not particularly secure, but it works and that's all I need right now.

Any better solutions are still welcome though...

I realize this is 2.5 years late but I think I have a better way... posted as answer. Feedback appreciated. – pete Mar 2 at 20:24.

I realize this is 2.5 years late (sorry! Didn't see it before...), but I use two static/shared methods to convert the row directly. Maybe this is what you're looking to do?

VB. Net: Public Shared Function GridViewRowToDataRow(ByVal gvr As GridViewRow) As DataRow Dim di As Object = Nothing Dim drv As DataRowView = Nothing Dim dr As DataRow = Nothing If gvr IsNot Nothing Then di = TryCast(gvr. DataItem, System.

Object) If di IsNot Nothing Then drv = TryCast(di, System.Data. DataRowView) If drv IsNot Nothing Then dr = TryCast(drv. Row, System.Data.

DataRow) End If End If End If Return dr End Function Public Shared Function GridViewRowEventArgsToDataRow(ByVal e As GridViewRowEventArgs) As DataRow Dim gvr As GridViewRow = Nothing Dim di As Object = Nothing Dim drv As DataRowView = Nothing Dim dr As DataRow = Nothing If e.Row. RowType = DataControlRowType. DataRow Then gvr = TryCast(e.

Row, System.Web.UI.WebControls. GridViewRow) dr = Helpers. GridViewRowToDataRow(gvr) End If Return dr End Function C#.

Net (Converted using: http://www.developerfusion.com/tools/convert/vb-to-csharp/): public static DataRow GridViewRowToDataRow(GridViewRow gvr) { object di = null; DataRowView drv = null; DataRow dr = null; if (gvr! = null) { di = gvr. DataItem as System.

Object; if (di! = null) { drv = di as System.Data. DataRowView; if (drv!

= null) { dr = drv. Row as System.Data. DataRow; } } } return dr; } public static DataRow GridViewRowEventArgsToDataRow(GridViewRowEventArgs e) { GridViewRow gvr = null; object di = null; DataRowView drv = null; DataRow dr = null; if (e.Row.

RowType == DataControlRowType. DataRow) { gvr = e. Row as System.Web.UI.WebControls.

GridViewRow; dr = Helpers. GridViewRowToDataRow(gvr); } return dr; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions