I currently copy the row to an empty datatable and bind that one, but surely there is a better way...
views:
322answers:
2
+2
Q:
What is the most simple way to bind a single row of a datatable to a detailsview in C# asp.net?
+1
A:
yes, it is the most simple way :)
IMHO, If you want to display only 1 field, you might use output parameters, but if you display more than one field in a detailsview it'!s the most simple way.
Canavar
2009-03-01 15:03:39
more fields... but thanks anyway :)
akosch
2009-03-01 15:07:03
After selecting a datatable, filtering over it is simpler ?
Canavar
2009-03-01 19:01:06
+2
A:
You don't need a data-table to bind - you just need something like a list / enumerable. For example, if you know the row number:
DataRowView row = dt.DefaultView[1]; // second row
detailsView1.DataSource = new DataRowView[] {row};
detailsView1.DataBind();
Note that we have to use DataRowView
(rather than DataRow
) in order to get the runtime-only properties (i.e. the data from columns). If you have a DataRow
, this approach could easily be wrapped in a utility method, for example an extension method:
public static DataRowView[] ForBinding(this DataRow row)
{
foreach (DataRowView rowView in row.Table.DefaultView)
{
if (ReferenceEquals(rowView.Row, row))
{
return new DataRowView[] { rowView };
}
}
throw new ArgumentException("Row not found in the default view", "row");
}
with:
detailsView1.DataSource = row.ForBinding();
detailsView1.DataBind();
Marc Gravell
2009-03-01 15:25:06