Binding an object to a table

  • Thread starter Thread starter shumaker
  • Start date Start date
S

shumaker

I've created objects before that I could bind to UI controls, but now
I want to do sort of the opposite. I want to bind an object to a
table such that the current rows columns map to properties of the same
name in the object.

I have been halfway successful by inheriting from the
BindableComponent class and then using code such as
court.DataBindings.Add("ColumnName1", codesCourtBindingSource,
"ColumnName1");

....where court is my object, codesCourtBindingSource has a DataSet as
its DataSource and the table name as it's DataMember.

However, this means I have to write a line of code for every column.
I have done bindings before where I could specify an empty string for
the property name and datamember, and it binds all the names which
match. In this scenario though, I haven't had any success with this:
court.DataBindings.Add("", codesCourtBindingSource, "");

I wanted to implement IBindableComponent so that I could intercept
calls and see what was going on under the hood with this binding
stuff, but I have no idea how to create this implementation and
haven't found any fleshed out examples.

If anyone has links to examples of implementing IBindableComponent,
I'd be interested.
 
Can I clarify what you mean?
such that the current rows columns map to properties of the same
name in the object.
Isn't that just
dataGridView.DataSource = myObject;
where myObject could be either a single entity or some form of list
(including binding-source)
(possibly with AutoGenerateColumns enabled)

I'm not entirely sure I followed what you were trying to do...

Marc
 
Can I clarify what you mean?> such that the current rows columns map to properties of the same

Isn't that just
dataGridView.DataSource = myObject;
where myObject could be either a single entity or some form of list
(including binding-source)
(possibly with AutoGenerateColumns enabled)

I'm not entirely sure I followed what you were trying to do...

Marc

Yes, mapping columns of a particular name in the table to properties
of the same name in the object.

But your example is using a UI control(the dataGridView) instead of a
datasource such as a DataSet. This is the opposite of what I'm trying
to do, because in your example the object is the datasource, and the
datagridview is a UI element. In my case the dataset is the source of
data which will populate my object.

In most examples you have a UI control bound to an object or dataset.
This is easy to do because most UI controls already implement
IBindableComponent or something similar. This is what gives it
the .DataBindings or .DataSource properties.

This is the layers of what I'm trying to do in my prototype:

In the data layer I have a strongly typed dataset for a single table.
I want to populate the object with a row of the table.

The UI layer has a single property grid and a single combobox.
I will wire up my propertyGrid to the currentObject something like
this:
propertyGrid.SelectedObject = currentCourtObject

This makes manipulating the data in the object very easy, and I like
this interface a lot.

So far my Court class(which is my simple object) inherits from
BindableComponent which gives it the DataBindings property that allows
me to wire it to the DataSet, but I have to do it one property at a
time similar to:
currentCourtObject.DataBindings.Add(new Binding("CodeText",
codesCourtBindingSource, "CodeText"));
currentCourtObject.DataBindings.Add(new Binding("CourtAddress",
codesCourtBindingSource, "CourtAddress"));
currentCourtObject.DataBindings.Add(new Binding("CourtName",
codesCourtBindingSource, "CourtName"));
currentCourtObject.DataBindings.Add(new Binding("CourtPhone",
codesCourtBindingSource, "CourtPhone"));

This seems silly, as all the names match. Additionally there seems to
be additonal functionality in the windows controls that causes them to
stay in sync with the currently selected record. I.E. if went the
more traditional route and I have the combobox and some textboxes
bound to the same bindingsource, then the textboxes dispaly whatever
record is selected in the combobox. The properties in my Court class
takes the place of the textboxes, so I would expect the same behavior,
but apperently the BindableComponent inheritance doesn't make my class
quite as functional as a textbox.

So there's several reasons why I want to see someone's example of a
concrete implementation of IBindableComponent, so that:
1. I have a better understanding of how binding works under the hood
2. I can put breakpoints in the implemented code and step through the
code to get a better understanding of what's going on.
3. I can hopefully improve the functionality of the binding.
 
OK - I now understand what you want to do...

I reemmber reading a good article on object-to-object (non-UI) binding
a while ago, which used server-side background processing as an
example ... I'll see if I can find it, but I've had a quick hunt and I
can't spot it... but the MSDN articles are normally quite good for
examples (IMO)...

Marc
 
OK - I now understand what you want to do...

I reemmber reading a good article on object-to-object (non-UI) binding
a while ago, which used server-side background processing as an
example ... I'll see if I can find it, but I've had a quick hunt and I
can't spot it... but the MSDN articles are normally quite good for
examples (IMO)...

Marc

Thanks Marc. I found a post with an IBindableComponent
implementation:
http://blogs.msdn.com/jfoscoding/archive/2005/12/23/507164.aspx

I was able to emulate this and it works well for a regular object, but
I never figured out how to get it to work without specifying a binding
for every single property. Maybe I will make an AutoBind function
that reflectively matches up properties and creates bindings for each
one so that I don't have to hardcode the property names.

There are several MSDN articles on binding objects to server side
controls for ASP.NET, but again that is using the objects as a
datasource instead of the object using a dataset as the datasource.

-Aaron
 
OK - I now understand what you want to do...

I reemmber reading a good article on object-to-object (non-UI) binding
a while ago, which used server-side background processing as an
example ... I'll see if I can find it, but I've had a quick hunt and I
can't spot it... but the MSDN articles are normally quite good for
examples (IMO)...

Marc

Thanks Marc. I found a post with an IBindableComponent
implementation:
http://blogs.msdn.com/jfoscoding/archive/2005/12/23/507164.aspx

I was able to emulate this and it works well for a regular object, but
I never figured out how to get it to work without specifying a binding
for every single property. Maybe I will make an AutoBind function
that reflectively matches up properties and creates bindings for each
one so that I don't have to hardcode the property names.

There are several MSDN articles on binding objects to server side
controls for ASP.NET, but again that is using the objects as a
datasource instead of the object using a dataset as the datasource.

-Aaron
 

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

Back
Top