gpt4 book ai didi

c# - 如何在代码隐藏中将 DataGrid 绑定(bind)到 DataTable?

转载 作者:行者123 更新时间:2023-12-03 03:00:45 25 4
gpt4 key购买 nike

如何在 C# 代码后面将 DataGrid 绑定(bind)到 DataTable? (所有控件都是在运行时生成的,因此请不要使用 XAML)

我尝试了 Binding()、设置 DataContext、设置 ItemsSource,但都不起作用:

/* Binding() fails */
Binding bind = new Binding();
bind.Source = dataset;
bind.Path = new PropertyPath("dataset.Tables");
datagrid.SetBinding(DataSet, bind);

/* DataContext fails */
datagrid.DataContext = dataset.Tables;

/* ItemsSource fails */
datagrid.ItemsSource = dataset.Tables;

我需要做的就是将 DataGrid 绑定(bind)到 DataTable,这样当 DataTable 中添加新行时,它将自动显示在 DataGrid 上。

我搜索了 stackoverflow 和 google,但奇怪的是我找不到解决方案。

最佳答案

尝试绑定(bind)到数据集中的表之一:

datagrid.ItemsSource = dataset.Tables[0].DefaultView;

关于c# - 如何在代码隐藏中将 DataGrid 绑定(bind)到 DataTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5815014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com