DataGridView and Two Array Sources


T

Trecius

Hello, Newsgroupians:

I have a ListBox that has a .DataSource of an array of Person[]. I am also
using a CurrencyManager to move the position when the Selected_Index of the
ListBox also changes. This works for text boxes and other controls as
follows...

this.TextBox1.DataBindings.Add("Text", lstPeople, "Name");

As you can see, the person's name will populate the textbox.

However, I have a DataGridView that I would like to populate as well. The
first two columns of the DataGridView come from a static dictionary: the key
is the identifier of the task, and the value is the task name. I also want
to add a third column to the DataGridView that comes from the Person
datatype. It will be a boolean value, indicating if the person has
successfully completed the task (True or False).

How can I make it so I that when the Person is changed in the Listbox, the
DataGridView will also change the third, or last, column?

On a short note, I can populate the first two rows, using my dictionary, as
follows:

BindingSource bind = new BindingSource();
bind.DataSource = dictTasks;
GridView1.DataSource = bind;

However, how should I get my last column in there that corresponds to the
Person completing the job or not? Thank you.


Trecius
 
Ad

Advertisements

T

Trecius

Well, it's not quite what I'm looking for.

When the actual user of the program selects a person that is found in the
ListBox, all the contents on the form change; for example, the First Name
TextBox, the LastName TextbBox. Etc.

It changes by the following event...

private void ListBoxPerson_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ListBoxPerson.SelectedIndex >= 0)
{
this.cm.Position = this.ListBoxPerson.SelectedIndex;
}
}

NOTE: this.cm is the CurrencyManager.

This allows the controls on the page to dynamically change. However, the
Person also has an Array of tasks that were completed. I'm trying to
dynamically change the DataGridView too with respect to the change of index
of the ListBoxPerson.]


Trecius

Peter Duniho said:
[...]
How can I make it so I that when the Person is changed in the Listbox,
the
DataGridView will also change the third, or last, column?

Do you mean when the specific Person object changes, or when the "person"
selected in your ListBox changes (i.e. the ListBox selection changes)?
Why is the CurrencyManager relevant at all?
On a short note, I can populate the first two rows, using my dictionary,
as
follows:

BindingSource bind = new BindingSource();
bind.DataSource = dictTasks;
GridView1.DataSource = bind;

However, how should I get my last column in there that corresponds to the
Person completing the job or not? Thank you.

Hard to say what the best approach is. I suppose you could create a
wrapper class that implements, for example, IBindingList, and then
provides a unified view onto the dictionary and associated data via a
specific element type. But whether that's most appropriate in your
situation, no one can say because you didn't bother to post a
concise-but-complete code example that reliably demonstrates what's going
on.

The short answer is: you'll need some kind of "change" event somewhere to
update things automatically based on changes elsewhere in your data. If
you simply need an update based on a ListBox selection change, that's
easy, as ListBox already exposes such "change" events. If you need an
update based on when a specific Person object changes, then either your
Person object itself needs to have a "change" event, or you need some
mediator that knows when the Person object changes and can be used to
raise an event or otherwise update the UI.

Of course, all of that is probably pretty obvious. But that's about as
much advice as anyone can give in response to such a vague question.

Pete
 
Ad

Advertisements

C

Cor Ligthert[MVP]

Trecius,

I am always curious if people are doing simple things difficult or that
there is a reason.

Why are you using the dictionary at all, is not simpler to create a class
Person and use that as the items from a generic list?

Cor
 

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