Can I programmatically add a row to datagrid WPF?

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

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

You should use ObservableCollection<NavItem>as a data source. Then just adding a new item to your collection will add it to the datagrid.

See the MSDN article .

+3
source

I do not know if this decision is correct, but I approached something similar, in despair:

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 datatable as itemssource:

  this.dataGridLines.ItemsSource = dataTable.DefaultView;

datatable, datatable , , ..

0

Source: https://habr.com/ru/post/1768401/


All Articles