Best way to reference a value in DataRowView

  • Thread starter Thread starter offwhite
  • Start date Start date
O

offwhite

When looping over items in the RowDataBound event for a GridView
(ASP.NET) I can access e.Row.DataItem which shows as a DataRowView.
The following code allows me to access the data for that row.

e.Row.DataItem, System.Data.DataRowView).Row.Item(0)

Is there a better way to access that data? This syntax looks very ugly
and potentially error prone.

Ideally I would simply use code like this...

e.Row.DataItem("id")

But I cannot seem to do that.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
Thanks for the response...

When I tried doing that in VB.NET with options set to strict it
complained about a late binding issue. So I am now doing this after I
check that e.Row.DataItem is not null (or Nothing).

Dim row As DataRow = CType(e.Row.DataItem, System.Data.DataRowView).Row

Then I can use the row as a DataRow as I want.

tbName.Text = CStr(row("Name"))

At least I can isolate the ugliness to 1 line and then use the simple
accessors on the row multiple times which is more readable.

But here is another issue I have with working with data like this.
When I walk it with the debugger I cannot see the data in the watch. I
have to cast the DataRowView to a DataRow before I can dig into the
values of the DataRow. Even the Immediate Window did not help me.

When I am dealing with GridViews and DataSets sometimes this is the
most direct way to get to the right rows and columns for the desired
data but it is very hard to navigate to the data with the debugger.

Brennan Stehling
http://brennan.offwhite.net/blog/

DataRowView has property Item, too.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

offwhite said:
When looping over items in the RowDataBound event for a GridView
(ASP.NET) I can access e.Row.DataItem which shows as a DataRowView.
The following code allows me to access the data for that row.

e.Row.DataItem, System.Data.DataRowView).Row.Item(0)

Is there a better way to access that data? This syntax looks very ugly
and potentially error prone.

Ideally I would simply use code like this...

e.Row.DataItem("id")

But I cannot seem to do that.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
Hi,

Well it's not a much better solution but I usually add the casts with the
expression in the watch window. This works in C#.

--
Dave Sexton

offwhite said:
Thanks for the response...

When I tried doing that in VB.NET with options set to strict it
complained about a late binding issue. So I am now doing this after I
check that e.Row.DataItem is not null (or Nothing).

Dim row As DataRow = CType(e.Row.DataItem, System.Data.DataRowView).Row

Then I can use the row as a DataRow as I want.

tbName.Text = CStr(row("Name"))

At least I can isolate the ugliness to 1 line and then use the simple
accessors on the row multiple times which is more readable.

But here is another issue I have with working with data like this.
When I walk it with the debugger I cannot see the data in the watch. I
have to cast the DataRowView to a DataRow before I can dig into the
values of the DataRow. Even the Immediate Window did not help me.

When I am dealing with GridViews and DataSets sometimes this is the
most direct way to get to the right rows and columns for the desired
data but it is very hard to navigate to the data with the debugger.

Brennan Stehling
http://brennan.offwhite.net/blog/

DataRowView has property Item, too.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

offwhite said:
When looping over items in the RowDataBound event for a GridView
(ASP.NET) I can access e.Row.DataItem which shows as a DataRowView.
The following code allows me to access the data for that row.

e.Row.DataItem, System.Data.DataRowView).Row.Item(0)

Is there a better way to access that data? This syntax looks very ugly
and potentially error prone.

Ideally I would simply use code like this...

e.Row.DataItem("id")

But I cannot seem to do that.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
Back
Top