C# 数据绑定到以编程方式创建的 DataTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/425424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:54:16  来源:igfitidea点击:

Databinding to a programmatically created DataTable

c#data-bindingado.net

提问by FlySwat

Suppose I have a datatable like this:

假设我有一个这样的数据表:

        DataTable dt = new DataTable("Woot");

        dt.Columns.AddRange(new DataColumn[]{
            new DataColumn("ID",typeof(System.Guid)),
            new DataColumn("Name",typeof(String))
            });

When I try to bind a control to it:

当我尝试将控件绑定到它时:

        this.txtName.DataBindings.Add("Text", _dtRow, "Name");

I get this exception:

我得到这个例外:

Cannot bind to the property or column Name on the DataSource. Parameter name: dataMember

无法绑定到 DataSource 上的属性或列 Name。参数名称:dataMember

Any idea why this works on a datatable created by a dataAdapter, but not on a programmaticly created one?

知道为什么这适用于由 dataAdapter 创建的数据表,而不适用于以编程方式创建的数据表吗?

回答by Igor Zelaya

shouldn't you reference dt instead of _dtRow?

你不应该引用 dt 而不是 _dtRow 吗?

For example:

例如:

this.txtName.DataBindings.Add("Text", dt, "Name");

EDIT:

编辑:

This code worked for me:

这段代码对我有用:

   DataTable dt = new DataTable("Woot");

    dt.Columns.AddRange(new DataColumn[]{
        new DataColumn("ID",typeof(System.Guid)),
        new DataColumn("Name",typeof(String))
    });

    DataRow r = dt.NewRow();
    r["ID"] = new Guid();
    r["Name"] = "AAA";
    dt.Rows.Add(r);

    r = dt.NewRow();
    r["ID"] = new Guid();
    r["Name"] = "BBB";
    dt.Rows.Add(r);

    dataGridView1.DataSource = dt;

    this.txtName.DataBindings.Add("Text", r.Table , "Name");

回答by BFree

OK, after messing with your code for a while, I kept getting stumped. Then I finally realized the issue. I'm assuming _dtRow is a DataRow. You need to reference the actual DataTable (dt).

好吧,在弄乱你的代码一段时间后,我一直被难住。然后我终于意识到了这个问题。我假设 _dtRow 是一个 DataRow。您需要引用实际的 DataTable (dt)。

this.textBox1.DataBindings.Add("Text", dt, "Name");

EDIT: After seeing your comment on Igor's post. If you bind to dt, then say for example if you have a datagridview bound to this DataTable, every time you select a different row, the textbox will change.

编辑:在看到您对 Igor 帖子的评论后。如果您绑定到 dt,那么例如,如果您有一个绑定到此 DataTable 的 datagridview,则每次选择不同的行时,文本框都会更改。

Here's the code that works for me:

这是对我有用的代码:

            DataTable dt = new DataTable("Woot");

            dt.Columns.AddRange(new DataColumn[]{
            new DataColumn("ID",typeof(System.Guid)),
            new DataColumn("Name",typeof(String))
            });


            dt.Rows.Add(Guid.NewGuid(), "John");
            dt.Rows.Add(Guid.NewGuid(), "Hyman");

            this.dataGridView1.DataSource = dt;

            this.textBox1.DataBindings.Add("Text", dt, "Name");

Change rows in the DGV and you'll see the textbox change text.

更改 DGV 中的行,您将看到文本框更改文本。

EIDT AGAINOK, time to hack it. This is how I got it to work:

EIDT 再次确定,是时候破解它了。这就是我让它工作的方式:

this.textBox1.DataBindings.Add("Text",_dtRow.ItemArray[1], ""); 

I used index 1, but you can use whatever index you need in the array.

我使用了索引 1,但您可以使用数组中所需的任何索引。