tags:

views:

102

answers:

1

I just want to add a new row, I have my datasource in objects in which I need to do some processing. i need something like below for wpf datagrid...

DataRow row = dataTable.NewRow();
foreach (NavItem item in record.Items)
{
row[item.FieldNo.ToString()] = item.RecordValue;
}
dataTable.Rows.Add(row);

A: 

I do not know if it is the right solution, but I came up to something like this, in desperation:

foreach (NavField field in this.Fields)
 {
      DataGridTextColumn column = new DataGridTextColumn();
      column.Header = field.FieldNo.ToString();

      //Some other logic
      // Hide non active and hidden fields
      if (!field.Active || !field.Show)
           column.Visibility = System.Windows.Visibility.Collapsed;

       grid.Columns.Add(column);
  }

Then I add the datatable as itemssource:

  this.dataGridLines.ItemsSource = dataTable.DefaultView;

If I set the datatable directly, it does not care about the columns from the datatable and autogenerate its own columns, don't know why..

Kervin
You also need to set: AutoGenerateColumns to false;
Kervin
So each time your item source changes in order that field.Show changes you would need to re-run the loop for adding the columns?
bjoshi
If you know what all columns you can have then you add those in XAML and bind visibility of those columns so that if in the datasource it later changed then it can be reflected. Or you can have your column collection and bind that to a DP of the view that can re-generate columns.
bjoshi
In fact for my situation, I have to repopulate the whole grid each time and I do not know the columns which are dynamic.
Kervin