tags:

views:

17

answers:

1

I'm not sure where to look for this one... I've got a viewmodel that has an underlying DataRow providing part of the model. I want to display this information as a single record, in a vertical layout. I planned to use the DataGrid because I want the user to be able to add/delete/rename rows right across the DataTable despite only looking at one record. I'm not quite sure how to achieve this though. Example of what I'm expecting is below:

Source Data Table
ID, Name, Value
1, One, 1
2, Two, 2

Expected in my UI would be a table looking like the following


ID | 1


Name | One


Value | 1


+1  A: 

You can expose the DataRow as a list of fields :

public class DataRowField
{
    public int Index { get; set; }
    public string Name { get; set; }
    public object Value { get; set; }
}


public IEnumerable<DataRowField> Fields
{
    get
    {
        return _dataRow.Table.Columns.Cast<DataColumn>()
               .Select((column, i) => new DataRowField
               {
                   Index = i,
                   Name = column.ColumnName,
                   Value = _dataRow[column]
               });
    }
}

Then you just need to bind your DataGrid to the Fields property

Thomas Levesque