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);
5
  • 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. Commented Oct 7, 2010 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.. Commented Oct 7, 2010 at 15:29
  • My datasource is in objects which require processing before displaying, I cannot bind it directly. Commented Oct 7, 2010 at 16:02
  • I can't just bind it to an object I created, with dynamic columns?? Commented Oct 7, 2010 at 16:34
  • stackoverflow.com/questions/704724/… Commented Sep 25, 2011 at 12:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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..

4 Comments

You also need to set: AutoGenerateColumns to false;
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?
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.
In fact for my situation, I have to repopulate the whole grid each time and I do not know the columns which are dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.