tags:

views:

322

answers:

2

I currently copy the row to an empty datatable and bind that one, but surely there is a better way...

+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
more fields... but thanks anyway :)
akosch
After selecting a datatable, filtering over it is simpler ?
Canavar
+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