向未绑定的Datagrid C#添加行

时间:2014-03-22 23:52:29

标签: c# .net wpf datagrid

如何将DataGridRow添加到未绑定的DataGrid?

我知道使用数据绑定到DataTable等是个好主意但是如果我想手动将一行添加到我已经添加了列的DataGrid中,我该怎么做?谷歌似乎没有任何答案,仅适用于DataGridViews。

此试用代码有何改进?

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);

我正在寻找一些相当于:

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);

2 个答案:

答案 0 :(得分:0)

DataGrid的{​​{1}}属性为Items。您可以使用ItemsControl方法在集合中添加新项目。

答案 1 :(得分:0)

使用items属性的Add方法,如Hamlet Hakobyan所述。

\编辑: 添加的内容取决于您要显示的类型。 programmatically add column & rows to WPF Datagrid会帮助你。