2

So in WinForms you can easily add a row, for example

dataGridView1.Rows.Add(user.Handle, c);

But in WPF, when I try to use a DataGrid there is no 'Rows' property. Is there any way to do this in WPF that doesn't consist of an insane amount of lines of code or a lot of messing with XAML?

Franckentien
  • 324
  • 6
  • 21
Meme Machine
  • 949
  • 3
  • 14
  • 28
  • Bind a DataTable to your DataGridView and add a new row in DataTable –  Jul 07 '16 at 01:49
  • Take a look at this sample, using List<> : http://www.wpf-tutorial.com/datagrid-control/details-row/ –  Jul 07 '16 at 01:52
  • Possible duplicate of [programatically add column & rows to WPF Datagrid](http://stackoverflow.com/questions/704724/programatically-add-column-rows-to-wpf-datagrid) – currarpickt Jul 07 '16 at 02:30

2 Answers2

3

if you are not binding it to any source (i.e DataTable, List etc) try

dataGridView1.Items.Add(new DataItem { Column1 = "a", Column2 = "b" });
KillerKidz
  • 180
  • 5
2

It's this simple :

// add a row    
DataGrid.Items.Add(new DataItem()); 

// add a column
DataGrid.Columns.Add(new DataGridTextColumn()); 

Please refer this link for more, http://wpf.codeplex.com/Thread/View.aspx?ThreadId=34065

Or if you don't like to add rows directly like that, use a collection as source. Bind the Grid to a List (Observable collection). Add items to that list. Result: new rows show up in the grid.

ViVi
  • 4,339
  • 8
  • 29
  • 52