78

我是 WPF 的新手。我只想知道我们应该如何以编程方式将列和行添加到 WPF 中的 DataGrid。我们过去在 Windows 窗体中执行此操作的方式。创建表列和行,并将其绑定到 DataGrid。

我相信 WPF DataGrid 与 ASP.net 和 Windows 表单中使用的有点不同(如果我错了,请纠正我)。

我有我需要在 DataGrid 中绘制的行数和列数,以便用户可以编辑单元格中的数据。

4

8 回答 8

77

以编程方式添加一行:

DataGrid.Items.Add(new DataItem());

以编程方式添加列:

DataGridTextColumn textColumn = new DataGridTextColumn(); 
textColumn.Header = "First Name"; 
textColumn.Binding = new Binding("FirstName"); 
dataGrid.Columns.Add(textColumn); 

查看WPF DataGrid 讨论板上的这篇文章以获取更多信息。

于 2009-04-01T19:57:30.283 回答
30

试试这个,它可以 100% 工作:以编程方式添加列和行:您首先需要创建项目类:

public class Item
        {
            public int Num { get; set; }
            public string Start { get; set; }
            public string Finich { get; set; }
        }

        private void generate_columns()
        {
            DataGridTextColumn c1 = new DataGridTextColumn();
            c1.Header = "Num";
            c1.Binding = new Binding("Num");
            c1.Width = 110;
            dataGrid1.Columns.Add(c1);
            DataGridTextColumn c2 = new DataGridTextColumn();
            c2.Header = "Start";
            c2.Width = 110;
            c2.Binding = new Binding("Start");
            dataGrid1.Columns.Add(c2);
            DataGridTextColumn c3 = new DataGridTextColumn();
            c3.Header = "Finich";
            c3.Width = 110;
            c3.Binding = new Binding("Finich");
            dataGrid1.Columns.Add(c3);

            dataGrid1.Items.Add(new Item() { Num = 1, Start = "2012, 8, 15", Finich = "2012, 9, 15" });
            dataGrid1.Items.Add(new Item() { Num = 2, Start = "2012, 12, 15", Finich = "2013, 2, 1" });
            dataGrid1.Items.Add(new Item() { Num = 3, Start = "2012, 8, 1", Finich = "2012, 11, 15" });

        }
于 2012-07-05T07:34:41.447 回答
28

我有同样的问题。向 WPF 添加新行DataGrid需要一个技巧。DataGrid依赖于项目对象的属性字段。ExpandoObject允许动态添加新属性。下面的代码解释了如何做到这一点:

// using System.Dynamic;

DataGrid dataGrid;

string[] labels = new string[] { "Column 0", "Column 1", "Column 2" };

foreach (string label in labels)
{
    DataGridTextColumn column = new DataGridTextColumn();
    column.Header = label;
    column.Binding = new Binding(label.Replace(' ', '_'));

    dataGrid.Columns.Add(column);
}

int[] values = new int[] { 0, 1, 2 };

dynamic row = new ExpandoObject();

for (int i = 0; i < labels.Length; i++)
    ((IDictionary<String, Object>)row)[labels[i].Replace(' ', '_')] = values[i];

dataGrid.Items.Add(row);

//编辑:

请注意,这不是组件的使用方式,但是,如果您只有以编程方式生成的数据(例如,在我的例子中:一系列特征和神经网络输出),它会大大简化。

于 2013-11-21T23:18:50.110 回答
12

我找到了一个在运行时添加列并绑定到DataTable.

不幸的是,以这种方式定义了 47 列,它绑定到数据的速度对我来说不够快。有什么建议么?

xml

<DataGrid
  Name="dataGrid"
  AutoGenerateColumns="False"
  ItemsSource="{Binding}">
</DataGrid>

xaml.cs 使用 System.Windows.Data;

if (table != null) // table is a DataTable
{
  foreach (DataColumn col in table.Columns)
  {
    dataGrid.Columns.Add(
      new DataGridTextColumn
      {
        Header = col.ColumnName,
        Binding = new Binding(string.Format("[{0}]", col.ColumnName))
      });
  }

  dataGrid.DataContext = table;
}
于 2011-01-19T06:05:40.900 回答
5

编辑:对不起,我不再有下面提到的代码。这是一个巧妙的解决方案,虽然很复杂。


我发布了一个示例项目,描述了如何将 PropertyDescriptor 和 lambda 委托与动态 ObservableCollection 和 DynamicObject 一起使用来填充具有强类型列定义的网格。

列可以在运行时动态添加/删除。如果您的数据不是具有已知类型的对象,您可以创建一个数据结构,该结构可以通过任意数量的列进行访问,并为每个“列”指定一个 PropertyDescriptor。

例如:

IList<string> ColumnNames { get; set; }
//dict.key is column name, dict.value is value
Dictionary<string, string> Rows { get; set; }

您可以这样定义列:

var descriptors= new List<PropertyDescriptor>();
//retrieve column name from preprepared list or retrieve from one of the items in dictionary
foreach(var columnName in ColumnNames)
    descriptors.Add(new DynamicPropertyDescriptor<Dictionary, string>(ColumnName, x => x[columnName]))
MyItemsCollection = new DynamicDataGridSource(Rows, descriptors) 

甚至更好,如果是一些真实的物体

public class User 
{
    public string FirstName { get; set; }
    public string LastName{ get; set; }
    ...
}

您可以指定强类型的列(与您的数据模型相关):

var propertyDescriptors = new List<PropertyDescriptor>
{
    new DynamicPropertyDescriptor<User, string>("First name", x => x.FirstName ),
    new DynamicPropertyDescriptor<User, string>("Last name", x => x.LastName ),
    ...
}

var users = retrieve some users

Users = new DynamicDataGridSource<User>(users, propertyDescriptors, PropertyChangedListeningMode.Handler);

然后,您只需绑定到用户集合,并在您指定它们时自动生成列。传递给属性描述符的字符串是列标题的名称。在运行时,您可以向“用户”添加更多 PropertyDescriptor,向网格添加另一列。

于 2012-01-17T09:38:33.220 回答
3

如果您已经有了数据绑定,John Myczek 的回答就完成了。
如果没有,如果您想指定数据源,我知道您至少有 2 个选项。(但是我不确定这是否符合大多数准则,例如 MVVM)

选项1:就像JohnB说的那样。但我认为你应该使用自己定义的集合而不是弱类型的 DataTable(没有冒犯,但你不能从代码中看出每列代表什么)

xml.cs

DataContext = myCollection;

//myCollection is a `ICollection<YourType>` preferably
`ObservableCollection<YourType>

 - option 2) Declare the name of the Datagrid in xaml

        <WpfToolkit:DataGrid Name=dataGrid}>

xaml.cs中

CollectionView myCollectionView = 
      (CollectionView)CollectionViewSource.GetDefaultView(yourCollection);
dataGrid.ItemsSource = myCollectionView;

如果您的类型定义了属性FirstName,那么您可以执行 John Myczek 指出的操作。

DataGridTextColumn textColumn = new DataGridTextColumn(); 
dataColumn.Header = "First Name"; 
dataColumn.Binding = new Binding("FirstName"); 
dataGrid.Columns.Add(textColumn); 

如果您不知道需要在 dataGrid 中显示的属性,这显然不起作用,但如果是这种情况,您将有更多问题需要处理,我相信这超出了这里的范围。

于 2011-05-19T11:58:26.087 回答
0

将 DataTable 绑定到 CodeBehind xaml中的 DataGridTextColumn

<DataGrid
  Name="TrkDataGrid"
  AutoGenerateColumns="False"
  ItemsSource="{Binding}">
</DataGrid>

xml.cs

  foreach (DataColumn col in dt.Columns)
  {
    TrkDataGrid.Columns.Add(
      new DataGridTextColumn
      {
        Header = col.ColumnName,
        Binding = new Binding(string.Format("[{0}]", col.ColumnName))
      });
  }

  TrkDataGrid.ItemsSource= dt.DefaultView;
于 2020-01-17T13:18:28.817 回答
0

如果您已经有了数据绑定,John Myczek 的回答就完成了。如果没有,如果您想指定数据源,我知道您至少有 2 个选项。(但是我不确定这是否符合大多数准则,例如 MVVM)

然后,您只需绑定到用户集合,并在您指定它们时自动生成列。传递给属性描述符的字符串是列标题的名称。在运行时,您可以向“用户”添加更多 PropertyDescriptor,向网格添加另一列。

于 2015-10-02T12:27:40.463 回答