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

C# 数据绑定到以编程方式创建的数据表,c#,data-binding,ado.net,C#,Data Binding,Ado.net,假设我有这样一个数据表: DataTable dt = new DataTable("Woot"); dt.Columns.AddRange(new DataColumn[]{ new DataColumn("ID",typeof(System.Guid)), new DataColumn("Name",typeof(String)) }); 当我尝试将控件绑定到它时: t

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

        DataTable dt = new DataTable("Woot");

        dt.Columns.AddRange(new DataColumn[]{
            new DataColumn("ID",typeof(System.Guid)),
            new DataColumn("Name",typeof(String))
            });
当我尝试将控件绑定到它时:

        this.txtName.DataBindings.Add("Text", _dtRow, "Name");
我得到一个例外:

无法绑定到属性或列 数据源上的名称。参数 姓名:dataMember


你知道为什么这对dataAdapter创建的datatable有效,而对程序创建的datatable无效吗?

你不应该引用dt而不是dtRow吗

例如:

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

这个代码对我有用:

   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");

你不应该引用dt而不是dtRow吗

例如:

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

这个代码对我有用:

   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");

好吧,在搞乱了你的代码一段时间后,我一直被难倒。然后我终于意识到了这个问题。我假设dtRow是一个数据行。您需要引用实际的数据表(dt)

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

以下是对我有效的代码:

            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(), "Jack");

            this.dataGridView1.DataSource = dt;

            this.textBox1.DataBindings.Add("Text", dt, "Name");
在DGV中更改行,您将看到文本框更改文本

EIDT再次好的,是时候破解了。我就是这样让它工作的:

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

我使用了索引1,但是你可以在数组中使用你需要的任何索引。

好的,在混乱了你的代码一段时间后,我一直被难倒。然后我终于意识到了这个问题。我假设dtRow是一个数据行。您需要引用实际的数据表(dt)

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

以下是对我有效的代码:

            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(), "Jack");

            this.dataGridView1.DataSource = dt;

            this.textBox1.DataBindings.Add("Text", dt, "Name");
在DGV中更改行,您将看到文本框更改文本

EIDT再次好的,是时候破解了。我就是这样让它工作的:

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

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

否,我绑定到的是datagrid中选择的单个行,而不是整个表。为什么不绑定到datatable呢?这样,如果在表中选择不同的行,文本属性将相应更改。我猜这就是数据绑定的全部目的…回到您最初的问题,我猜引发异常的原因是因为DataTable实现了IListSource接口(返回一个列表),而DataRow没有。不,我绑定到datagrid中选择的单个行,不是整个表。为什么不绑定到datatable呢?这样,如果在表中选择不同的行,文本属性将相应更改。我想这就是数据绑定的全部目的…回到您最初的问题,我想引发异常的原因是因为DataTable实现了IListSource接口(返回列表),而DataRow没有。绑定到表可以防止文本框发生更改。我试过了,我也试过了。我有您发布的代码,但我也有绑定到DataGridView的DataTable。每次我在DataGridView上更改行时,文本框中的文本都会更改。问题是,实际的DataTable被抽象为一个表单,而它位于对DT不可见的用户控件中,因此只有CurrentRow属性。(别怪我,这个应用不是我设计的)。CurrentRow.Table绑定,但不会更新。绑定到该表可防止文本框更改。我试过了,我也试过了。我有您发布的代码,但我也有绑定到DataGridView的DataTable。每次我在DataGridView上更改行时,文本框中的文本都会更改。问题是,实际的DataTable被抽象为一个表单,而它位于对DT不可见的用户控件中,因此只有CurrentRow属性。(别怪我,这个应用不是我设计的)。CurrentRow.Table已绑定,但不会更新。