Nick LaMarca
Nick LaMarca

Reputation: 8188

How To Get A Value Out Of A DetailsView DataItem Property

I am accessing a DetailsView from the following event

 public void dvDetails_DataBound(Object sender, EventArgs e)

I am casting the sender to my detailsview like so

DetailsView dv = (DetailsView)sender;

Now when I look in "dv" I can see the DataItem property has the data I want in it under a field name, but I dont know how to write the code access the value??

The field name is shown in the dataitem property as "_DTMON_F", I tried to say

Datetime myDate=dv.DataItem["_DTMON_F"]

BUT C# doesnt like the syntax, can someone help me with this?

Upvotes: 0

Views: 6172

Answers (3)

mellamokb
mellamokb

Reputation: 56769

DataRowView drv = dv.DataItem as DataRowView;
DateTime myDate = (DateTime)drv["_DTMON_F"];

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460148

That depends on the Datasource of your DetailsView. In case of a SqlDataSource DataItem would be a DataRowView. You have to cast it, then you can access it's column. For example:

Datetime myDate=(DateTime)((DataRowView)dv.DataItem)["_DTMON_F"];

Upvotes: 1

Jamie Treworgy
Jamie Treworgy

Reputation: 24344

Casting problem perhaps?

DateTime myDate=(DateTime)dv.DataItem["_DTMON_F"];

If the data source is a database, you may need to use Convert.

Upvotes: 0

Related Questions