简体   繁体   中英

Add row to unbound Datagrid C#

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM