Question about Data Binding

M

Markward

Hello everybody!

I am struggeling with data binding.
Reading the book .NET Compact Framework Programming with C# from Paul
Yao and David Durand, I got the impression, that data binding is a
technique, that is not only for Database-Objects, but also works well
with ArrayLists and such.

My Scenario ist now, that I have an ArrayList of my custom Data-Object
( a struct ). Associating the ArrayList with a ListBox or a ComboBox
works well, also the boxes are populated with the ArrayList-Items at
first.

Also the simple binding throught the boxes to Single-Value Controls
works as far, as "some" data is displayed.

My two problems are:

1.
The Controls bound by simple binding allways access the very first item
of the ArrayList, ignoring whatever item in the box is chosen. This
appears strange to me, as each of the two boxes is notified of a
changed selection in the other box.


2.
Changes made to the simple-bound controls are propagated to the
ArrayList, but don`t reflect in the Components - no update of the
visual representation.

So my questions are:
Is databinding fully functional with ArrayLists of custom Objects ?
If not, is there any workaround, that is nice programming style ?
Is data binding only für Databases-Applications, and simple scenarios
like mine should be dealt with like :

for ......{
combobox.Items.Add(..);
}


Thanks for any replys !

Markward
 
G

Ginny Caughey [MVP]

Markward,

If you want multiple controls on a form to point to the same item in the
ArrayList, they should all have the same datasource. Data binding works fine
for any collection that implement IList or IListSource and not just
databases. If this doesn't answer your questions, please show us a litle
code that illustrates how your controls are databound.

Ginny Caughey
..NET Compact Framework MVP

Hello everybody!

I am struggeling with data binding.
Reading the book .NET Compact Framework Programming with C# from Paul
Yao and David Durand, I got the impression, that data binding is a
technique, that is not only for Database-Objects, but also works well
with ArrayLists and such.

My Scenario ist now, that I have an ArrayList of my custom Data-Object
( a struct ). Associating the ArrayList with a ListBox or a ComboBox
works well, also the boxes are populated with the ArrayList-Items at
first.

Also the simple binding throught the boxes to Single-Value Controls
works as far, as "some" data is displayed.

My two problems are:

1.
The Controls bound by simple binding allways access the very first item
of the ArrayList, ignoring whatever item in the box is chosen. This
appears strange to me, as each of the two boxes is notified of a
changed selection in the other box.


2.
Changes made to the simple-bound controls are propagated to the
ArrayList, but don`t reflect in the Components - no update of the
visual representation.

So my questions are:
Is databinding fully functional with ArrayLists of custom Objects ?
If not, is there any workaround, that is nice programming style ?
Is data binding only für Databases-Applications, and simple scenarios
like mine should be dealt with like :

for ......{
combobox.Items.Add(..);
}


Thanks for any replys !

Markward
 
M

Markward

Thanks for the fast response.

In fact, my Components have the same dataSource..
Here is some code ...

Here is the code for my custom data-object :

public struct Patient {

public string uniqueId;
public string forename;
public string name;
public string street;
public string plz;
public string city;

public string patientName {
get{
return this.name;
}
set{
this.name = value;
}
}

/*more properties are defined here ....*/

}

These data-objects are stored in an ArrayList ..

public static ArrayList PatientenListe = new ArrayList();

This is my listbox, which lives on Form1.

// Called in the forms constructor Form1

cBox.DataSource = Global.PatientenListe;
cBox.ValueMember = "patientUid";
cBox.DisplayMember = "patientName";

On Form1 one can select an Item in the listbox and click on a
"edit"-button, which displays Form2 with a couple of
SingleValue-Controls, representing all the different datafields of the
selected list-item.


The binding works as follows ...

// Called in the forms constructor Form2

textboxName.DataBindings.Add("Text",Global.patientenListDlg.cBox.DataSource,"patientName");
textboxForename.DataBindings.Add("Text",Global.patientenListDlg.cBox.DataSource,"patientForename");
textboxStreet.DataBindings.Add("Text",Global.patientenListDlg.cBox.DataSource,"patientStreet");
....

If you wonder, I made the cBox ( comboBox ) a public member of the
Form1, so it is accessible from within Form2

When I click the "edit"-button, Form2 opens and is populated with the
values of ArrayList-Item number 1, no matter, what Item was selected in
the Combobox.
When changing the values, the changes are made in the ArrayList also,
which I checked with the debugging variable-inspector, but no Update of
the comboBox is done. This would be expected, as I changed the values,
that make the comboBox`s DisplayMember.

May be I misunderstood, how the binding works....

I thought, that I bind Controls to a DataSource once, and then Updating
is done automatically....

Hope, I could make my problem a bit clearer...
 
G

Ginny Caughey [MVP]

Markward,

Do you get a different result if you use something like this for the
bindings that you want to update:

textboxName.DataBindings.Add("Text",Global.patientenListDlg.cBox.DataSource,"patientName",
false, DataSourceUpdateMode.OnValidation);

This works with DataSet (which doesn't care how the data got loaded) and
should theoretically work on any IList collection, but I haven't tested it
with an ArrayList.

Ginny Caughey
..NET Compact Framework MVP
 
G

Guest

Ginny,

I'm having a combobox/data binding issue also and like Markward, don't seem
to find a solution reading several texts. I am binding an arraylist to to
combobox. The binding seems to go okay and I have scrollup and scrolldown
buttons on the form to change the selected item. However, only one item is
visible at a time rather than the whole list. Of course, the box is sized
for multiple items.

Thanks in advance.

drumred
 
G

Ginny Caughey [MVP]

What does the databinding code look like for your combobox? It's possible
that what you see is what you'll get unless you manage your own data binding
manually, but a code sample might help.

Ginny Caughey
..NET Compact Framework MVP
 

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