3

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);
dontbyteme
  • 1,221
  • 1
  • 11
  • 23
Kervin Ramen
  • 2,575
  • 2
  • 24
  • 22
  • 3
    Why don't you bind your datasource to the wpf datagrid's itemssource and then when your datasource is updated (as in new row is added) it will be added in the wpf datagird as well. – Bhupendra Oct 07 '10 at 11:05
  • I tried to bind the datatable to the itemssource, it works, but the header, the visiblity and other mapping with the wpf datagrid are not correct. I tried a dictionary list, in which I wanted the key to be the column and the value the row value, which does not work. I am running out of ideas, all resources on the web, are with static columns.. – Kervin Ramen Oct 07 '10 at 15:29
  • My datasource is in objects which require processing before displaying, I cannot bind it directly. – Kervin Ramen Oct 07 '10 at 16:02
  • I can't just bind it to an object I created, with dynamic columns?? – Kervin Ramen Oct 07 '10 at 16:34
  • http://stackoverflow.com/questions/704724/programatically-add-column-rows-to-wpf-datagrid – Mohsen Sep 25 '11 at 12:32

2 Answers2

3

You should be using an ObservableCollection<NavItem> as the datagrid source. Then simply adding a new element to your collection will add it to the datagrid.

See this MSDN article.

Mart
  • 5,608
  • 3
  • 32
  • 45
0

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 Ramen
  • 2,575
  • 2
  • 24
  • 22
  • You also need to set: AutoGenerateColumns to false; – Kervin Ramen Oct 08 '10 at 06:56
  • 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? – Bhupendra Oct 12 '10 at 16:25
  • 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. – Bhupendra Oct 13 '10 at 04:07
  • 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 Ramen Oct 13 '10 at 06:33