【问题标题】:How do I add row data directly to a DataGrid Class?如何将行数据直接添加到 DataGrid 类?
【发布时间】:2011-11-30 14:40:21
【问题描述】:

如何将行数据直接添加到 DataGrid 类?

我正在使用一家公司提供的免费开源课程,我不会说出我非常喜欢的公司名称(即使它是 RadiantQ)。它有一个很酷的 MuLtiColumnTreeList 控件,它是树控件和数据网格的组合。它带有一个示例代码,您可以查看所有内容。这很酷。它继承自 DataGrid 类。

问题是我对这个级别的数据绑定有点陌生,所以我想继续编写一些代码,将我从另一个类收集的数据强制到控件中。

所以我在网上寻找如何为 DataGrid 类执行此操作,但信息不容易获得。有人可以帮忙吗?

似乎一旦数据绑定完成,如果您更改数据,您必须重新绑定到控件。这就是以前给我带来困难的原因。所以我要做的就是运行这样的命令:

this.mutlicoolgridview.ItemsSource = null; this.mutlicoolgridview.ItemsSource = SampleData.GetSampleDataNew();

我现在遇到的问题是这样的。在运行他的命令大约一千次之后,我实际上耗尽了内存。我认为这样做:

this.mutlicoolgridview.ItemsSource = null;

不是一个好主意。有没有更好的命令来释放内存?

这是一个类似的崩溃: []

【问题讨论】:

    标签: c# silverlight c#-4.0 silverlight-4.0 datagrid


    【解决方案1】:

    要将行添加到 DataGrid,您需要首先将 DataSource 绑定到 DataGrid,然后将行添加到 DataSource。

    有效的数据源是:

    • 一个数据表
    • 一个数据视图
    • 一个数据集
    • DataViewManager
    • 一维数组
    • 任何实现 IListSource 接口的组件
    • 任何实现 IList 接口的组件

    这是一个向绑定到 DataGrid 的 DataTable 添加行的 Windows 窗体示例:

    public partial class Form1 : Form
    {
        // Instantiate the DataSource that will be bound to the DataGrid
        DataSet dataSet = new DataSet("MyDataSet");
        DataTable dataTable = new DataTable("MyDataTable");
    
        public Form1()
        {
            InitializeComponent();
    
            this.dataSet.Tables.Add(this.dataTable);
            this.dataTable.Columns.Add(new DataColumn("Date"));
    
            // Bind the DataTable to the DataGrid
            this.dataGrid1.SetDataBinding(this.dataSet, "MyDataTable");
        }    
    
        private void button1_Click(object sender, EventArgs e)
        {
            // When the user clicks the button, add a new row to the DataTable
            DataRow dr = this.dataTable.NewRow();
            dr["Date"] = DateTime.Now;
            this.dataTable.Rows.Add(dr);
        }
    }
    

    我建议您创建一个一次性项目并使用 DataGrid 类来熟悉 DataGrid 使用 DataSource 的不同方式。

    【讨论】:

    • DataGrid 没有这些行。如果这样做会简单得多。
    • @xarzu - 我在哪里说过 DataGrid 有“这些行”? - 您会在我的回答中注意到,我解释说您需要将行添加到绑定到 DataGrid 的数据源中。在您的 OP 中,您说“所以我在网上寻找如何为 DataGrid 类执行此操作,并且信息不容易获得。有人可以帮忙吗?” - 在我的示例中,我为您提供了如何将一行数据添加到 DataGrid 的线索。当您对回答您的问题的答案投反对票时,请尝试更加小心 - 即,也许您应该考虑改写您的问题。
    • @Jed 该问题用 Silverlight 标记,因此 WinForms 答案可能不适用。
    • 谢谢你让我明白,Murven。
    • 似乎一旦数据绑定完成,如果您更改数据,您必须重新绑定到控件。这就是以前给我带来困难的原因。所以我要做的是运行这样的命令:this.mutlicoolgridview.ItemsSource = null; this.mutlicoolgridview.ItemsSource = SampleData.GetSampleDataNew();我现在遇到的问题是这个。在运行他的命令大约一千次之后,我实际上耗尽了内存。我认为这样做: this.mutlicoolgridview.ItemsSource = null;这不是一个好主意。有没有更好的命令来释放内存?
    【解决方案2】:

    如果您有一个对象列表,您可以将它们复制到一个 BindingList。然后你可以使用

    dataGrid.ItemsSource = myBindingList;
    

    【讨论】:

    • 我试过了,我的输出窗口显示了各种错误。似乎数据和源不匹配,这很奇怪,因为示例代码显示了这个奇怪的东西,其中有另一个接受绑定列表的控件,并且多列树列表应该以某种方式绑定到那个/
    • 项目源可以是数据表,也可以是对象列表。将出现的列是对象的公共属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多