问题描述
我是 WPF 的新手.我只想知道我们应该如何以编程方式将列和行添加到 WPF 中的 DataGrid.我们过去在 Windows 窗体中使用的方式.创建表列和行,并将其绑定到 DataGrid.
I'm new to WPF. I just want to know how should we add columns and rows programmatically to a DataGrid in WPF. The way we used to do it in windows forms. create table columns and rows, and bind it to DataGrid.
我相信 WPF DataGrid 与 ASP.net 和 Windows 表单中使用的有点不同(如果我错了,请纠正我).
I believe WPF DataGrid is bit different the one used in ASP.net and Windows form (correct me if I am wrong).
我有需要在 DataGrid 中绘制的行数和列数,以便用户可以编辑单元格中的数据.
I have No. of rows and columns which I need to draw in DataGrid so that user can edit the data in the cells.
推荐答案
以编程方式添加一行:
DataGrid.Items.Add(new DataItem());
以编程方式添加一列:
DataGridTextColumn textColumn = new DataGridTextColumn();
textColumn.Header = "First Name";
textColumn.Binding = new Binding("FirstName");
dataGrid.Columns.Add(textColumn);
查看 WPF DataGrid 讨论板上的这篇文章以了解更多信息信息.
Check out this post on the WPF DataGrid discussion board for more information.
这篇关于以编程方式添加列 &行到 WPF 数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!