Databinding and Dataset

G

Guest

Hi,

I am trying to determine the name of the datatable that a control is bound
to using code. For example: I have a textbox which is bound to a
bindingsource. The bindingsource is either bound to a dataset's datatable or
another bindingsource and a relation (typical master detail setup). How can
I determine the name of the underlying datatable that the control is bound to?

Scenario 1: textbox, bound to bindingsource, bindingsource datasource is a
dataset, datamember is a datatable.
Scenario 2: textbox is bound to a bindingsource, bindingsource is bound to
another bindingsource - datasource is binding source, datamember is a
relation such as master_detail.

Thank you very much for your help.
 
B

Bart Mermuys

Hi,

kzoltan said:
Hi,

I am trying to determine the name of the datatable that a control is bound
to using code. For example: I have a textbox which is bound to a
bindingsource. The bindingsource is either bound to a dataset's datatable
or
another bindingsource and a relation (typical master detail setup). How
can
I determine the name of the underlying datatable that the control is bound
to?

Scenario 1: textbox, bound to bindingsource, bindingsource datasource is
a
dataset, datamember is a datatable.
Scenario 2: textbox is bound to a bindingsource, bindingsource is bound
to
another bindingsource - datasource is binding source, datamember is a
relation such as master_detail.

You can get the BindingSource from a Control property binding like this:

BindingSource bs = (BindingSource)
someTextBox.DataBindings["Text"].DataSource;


Once you have the BindingSource you can get the name of the underlying
DataTable:

Console.WriteLine( bs.GetListName(null) );
-or-
Console.WriteLine( ((DataView)bs.List).Table.TableName );


HTH,
Greetings
 
G

Guest

Thank you for the response.
Unfortunately, the GetListName returns "IBindingList" instead of the table
name. If I do bs.List I get an invalid cast, since bs.List is an
IBindingList.


Bart Mermuys said:
Hi,

kzoltan said:
Hi,

I am trying to determine the name of the datatable that a control is bound
to using code. For example: I have a textbox which is bound to a
bindingsource. The bindingsource is either bound to a dataset's datatable
or
another bindingsource and a relation (typical master detail setup). How
can
I determine the name of the underlying datatable that the control is bound
to?

Scenario 1: textbox, bound to bindingsource, bindingsource datasource is
a
dataset, datamember is a datatable.
Scenario 2: textbox is bound to a bindingsource, bindingsource is bound
to
another bindingsource - datasource is binding source, datamember is a
relation such as master_detail.

You can get the BindingSource from a Control property binding like this:

BindingSource bs = (BindingSource)
someTextBox.DataBindings["Text"].DataSource;


Once you have the BindingSource you can get the name of the underlying
DataTable:

Console.WriteLine( bs.GetListName(null) );
-or-
Console.WriteLine( ((DataView)bs.List).Table.TableName );


HTH,
Greetings

Thank you very much for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top