0

How can I add a DataGridRow to an unbound DataGrid?

I know it's a good idea to use data binding to a DataTable, etc. but if I wanted to add a Row manually to a DataGrid that I've already added columns to, how would I do that? Google doesn't seem to have any answers, only for DataGridViews.

Any improvement on this trial code?

Datagrid_Main.AutoGenerateColumns = false;
for (int i = 0; i < 6; i++)
{
   DataGridTextColumn dc1 = new DataGridTextColumn();
   dc1.Header = i.ToString();
   Datagrid_Main.Columns.Add(dc1);
}

DataGridRow dgr1 = new DataGridRow();
Datagrid_Main.Items.Add(dgr1);

I'm looking for some equivalent to:

DataGridViewRow row = (DataGridViewRow)walletDataGridView.Rows[0].Clone();
row.Cells[0].Value = "Value";
row.Cells[1].Value = "Test1";
row.Cells[2].Value = "12.01";
walletDataGridView.Rows.Add(row);
user3329538
  • 289
  • 1
  • 5
  • 12

2 Answers2

0

DataGrid has an Items property which is ItemsControl. You can use Add method to add new Item the the collection.

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

Use the Add method of the items property, like Hamlet Hakobyan mentioned.

\edit: What you add depends on the type yout want to display. programmatically add column & rows to WPF Datagrid will help you.

Community
  • 1
  • 1
devmb
  • 805
  • 6
  • 18
  • That's quite useful thanks. Is there any way to make a DatagridRow based on the template of the existing DataGrid like you can for a DataGridView? – user3329538 Mar 23 '14 at 12:49
  • You can define usual DataTemplates in XAML. Once define you can simply bind your data to the Grid. You should also be able to add row programmaticly. This may help you http://wpftutorial.net/DataGrid.html – devmb Mar 23 '14 at 15:19
  • please see my revised question – user3329538 Mar 24 '14 at 18:34
  • I don't understand you question, didn't you see my answer? :) – devmb Apr 09 '14 at 21:23