views:

672

answers:

2

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

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

+1  A: 

shouldn't you reference dt instead of _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");
Igor Zelaya
No, I'm binding to an individual row that is selected in a datagrid, not the whole table.
FlySwat
Why not bind to the datatable instead? That way if you select a different row in the table the text property will change accordingly. I guess that is the whole purpose of databindings...
Igor Zelaya
Returning to your original question, I guess the reason why the exception raises is beacause DataTable implements IListSource interface (wich returns a list) and DataRow does not.
Igor Zelaya
+3  A: 

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).

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.

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

            this.dataGridView1.DataSource = dt;

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

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

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

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

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

BFree
Binding to the table prevents the textboxes from changing. I just tried it.
FlySwat
I just tried it too. I have the code you posted, but I also have the DataTable bound to a DataGridView. Every time I change rows on the DataGridView, the text in the textbox changes.
BFree
The problem is that the actual DataTable is abstracted away in a form, while this is in a user control with no visibility to the DT, so just a CurrentRow property. (Don't blame me, I didn't design this app). CurrentRow.Table binds, but won't update.
FlySwat
Yeap, That did the trick....
Igor Zelaya