[英]How to create N number of columns programmatically using C# with WPF DataGrid
I need to create N columns in WPF DataGrid where each column's header = "i" (i = 1..N)
and then to add items to each column.我需要在 WPF DataGrid 中创建 N 列,其中每列的
header = "i" (i = 1..N)
,然后向每列添加项目。 I got a problem when tried to implement it:我在尝试实现它时遇到了问题:
for (int i = 0; i < N; i++)
{
var col = new DataGridTextColumn();
col.Header = i.ToString();
col.Binding = new Binding(i.ToString());
dataGrid.Columns.Add(col);
dataGrid.Items.Add(new { i = "some txt" });
}
The problem may be at the line dataGrid.Items.Add(new { i = "some txt" });
问题可能出在
dataGrid.Items.Add(new { i = "some txt" });
to which I don't know how to fix it.我不知道如何解决它。 At this line of code IDE tells i is an anonymous type, i is a number and not a property.
在这行代码中,IDE 告诉i是一个匿名类型, i是一个数字而不是一个属性。
How can I add items to a column in this case?在这种情况下,如何将项目添加到列中?
You can create a DataTable and add your columns to the DataTable.您可以创建一个 DataTable 并将您的列添加到 DataTable。 Then set that DataTable as the data source of your data grid view.
然后将该 DataTable 设置为数据网格视图的数据源。
DataTable dt = new DataTable();
for(int i = 0; i < numberOfRows; i++){
DataRow row = dt.NewRow();
for(int c = 0; c < numberOfColumns; c++){
row[c] = "Your data here"
}
dt.Rows.Add(row);
}
MyDataGrid1.DataSource = dt;
Binding to index should have format: [index]
.绑定到索引应该有格式:
[index]
。 Also you should add items after you created columns:您还应该在创建列后添加项目:
for (int i = 0; i < N; i++)
{
var col = new DataGridTextColumn
{
Header = i.ToString(),
Binding = new Binding("[" + i + "]"),
IsReadOnly = true,
Width = new DataGridLength(1, DataGridLengthUnitType.Star)
};
dataGrid.Columns.Add(col);
}
var rowData = Enumerable.Range(0, N).ToList();
dataGrid.Items.Add(rowData);
Although I was able to build your demo similarly (using VS2017), I don't see it as practical as it does not show anything as no binding source of a specific type you are trying to set your binding context to.尽管我能够类似地构建您的演示(使用 VS2017),但我认为它并不实用,因为它没有显示任何内容,因为您尝试将绑定上下文设置为没有特定类型的绑定源。
Instead, here is something you might find useful.相反,这里有一些你可能会觉得有用的东西。 If you have a collection (List, ObservableCollection, etc.) that you are querying or otherwise populating, and you want that to automatically build, you can do with reflection.
如果您有一个正在查询或以其他方式填充的集合(List、ObservableCollection 等),并且您希望它自动构建,则可以使用反射来完成。 So example, I have a custom class
例如,我有一个自定义类
public class MySamplePerson
{
public string FirstName {get; set;}
public string LastName {get; set;}
public int Age {get; set;}
}
Then, in your method you prepare a list of these things然后,在你的方法中,你准备了这些东西的清单
private void PrepareYourGrid()
{
// build a sample list of the data and I'm populating with 3 entries
var SampleData = new List<MySamplePerson>();
SampleData.Add(new MySamplePerson{ FirstName = "Bill", LastName = "Board", Age = 62});
SampleData.Add(new MySamplePerson{ FirstName = "Eileen", LastName = "Dover", Age = 32 });
SampleData.Add(new MySamplePerson{ FirstName = "Ben", LastName = "Dover", Age = 33});
// using reflection, I am taking the first instance within the list,
// getting the type, and then getting a list of all the properties on that class.
var pi = SampleData[0].GetType().GetProperties();
// Now, for each property, go through and create a column
foreach( var onePI in pi)
{
var col = new DataGridTextColumn();
// header based on the property name and binding to that same column name,
// not just some fixed "text" value you prove
col.Header = onePI.Name;
col.Binding = new Binding(onePI.Name);
// now add the column to the grid
MyDataGrid1.Columns.Add(col);
}
// Now I can set the ItemsSource for the grid to the list of records I created
MyDataGrid1.ItemsSource = SampleData;
}
In my sample, my form data grid was named "MyDataGrid1".在我的示例中,我的表单数据网格被命名为“MyDataGrid1”。 Now if you run, the grid will show as many rows as you have and each of the public properties exposed too..
现在,如果您运行,网格将显示与您拥有的行数一样多的行,并且每个公共属性也会公开..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.