List Control Binding requires two data sources

E

emzyme20

Hi,

I am trying to populate a list control that is bound to a data table
but the display member needs to come from a different data table.

I have two list controls in C#, one displaying available categories and
one displaying selected categories. In the database these are
represented in two tables, the available category table has an ID and a
Name. The other table is a category links table that has an ID,
Category_ID and a Book_ID.

The first list control works fine with the following code and it
successfully displays the names of the categories:

// Specify bindings for the available categories.
m_lstAvailableCategories.DataSource = m_dtCategories;
m_lstAvailableCategories.DisplayMember = "Category_Name";
m_lstAvailableCategories.ValueMember = "Category_ID";
m_lstAvailableCategories.DataBindings.Add("Text", m_dtCategories,
"Category_ID");

My problem occurs when displaying the selected categories in the other
list control. This data is gathered from the category links table. I
have applied a row filter on the table to leave me only the rows that
match the Book_ID that I have specified. Ideally I would like to
display the Category Name in the list control but the problem I have is
that it does not know the category name, all I have is the category ID
and the Book ID. I think my problem is that the DisplayMember value
must reside in the table associated with the DataSource and this list
has its DataSource set to the category links table. I am getting the
correct rows returned, but all it displays is the Category IDs and that
is not any help to me.

I have this code so far and it's not working as expected, can anyone
point me in the right direction or is this just not possible?

// Specify bindings for selected categories.
m_lstSelectedCategories.DataSource = m_dtCatLinks;
m_lstSelectedCategories.DisplayMember = "Category_Name";
m_lstSelectedCategories.ValueMember = "Category_ID";
m_lstSelectedCategories.DataBindings.Add("Text", m_dtCategories,
"Category_ID");

Regards,

Emma
 
E

Emma Middlebrook

I've been thinking about this problem a little bit more and I have a
feeling my problem is not that I need to bind my list control to two
datasources and then use the rowFilter to display the categories that
only interest me for a particular book. Instead, maybe the selected
categories list control should be unbound and I populate it myself
every time a book is selected performing a lookup on both the data
tables to obtain the details I need to display.

It would be nice if there was a clever way I could have it bound as it
makes updating the database much easier.

Does anyone have any suggestions that might help me make the right
choice?

Regards,

Emma
 
B

Bruce Wood

I built something like this a while back. Some thoughts (assuming that
you're programming WinForms and not WebForms).

First, I believe that Microsoft's way of doing this is with a ListView
with Checkboxes. At least, that's what I'm using now. That's why the
two data sources: what is showing, and what is currently selected. They
do it all within one control.

Back when I had two lists instead of a single ListView with checkboxes,
what I did was build my own UserControl that contained the two lists
(exactly as you described) and the controls necessary to move items
from the "unselected" list to the "selected" list and vice versa. Your
UserControl could then expose the appropriate properties for the
binding (i.e. all items and selected items) and take care of mediating
between data and display (i.e. Category ID versus Name, etc).
Practically speaking, you could tailor the UserControl to the binding,
rather than trying to jigger the binding to your controls.
 
E

Emma Middlebrook

Thanks for the suggestion, I'll take a look at building my own user
control (I've not done that before).. The checked list box is a good
idea but I don't think it would quite work for what I wanted to do..
The categories can have sub-categories that are more specialised, that
you can drill down into. e.g. Sports could have the sub-categories
Football and Rugby. I could have a book that has a selected category of
sports and I could have another book that has been assigned a more
specific category of Football.

The only way I could think of displaying that, is to have buttons that
drill down into a category etc, and then you select the categories
(there could be multiple) that apply to a book. I wanted to see at a
glance, what categories have been assigned to a book and I don't think
I could do that easily with a checked list box.

Thanks,

Emma
 
E

Emma Middlebrook

I've just been looking at the user defined control and that will
basically just allow me to put all the category functionality into one
new form that will then I guess get embedded into my main form. I am
still not sure I'd be able to bind my list controls to the database
tables that I would like to populate them from. The available
categories is easy enough but because the selected categories requires
the details from the category links table to identify what categories
were chosen by this particular book and also the ability to be able to
lookup the category name from the category table that has been bound to
the available categories.

I still think I need to manually manage the contents of the selected
list.
 

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