massively confused about ListControl and its programming model

D

David

I am used to working with DropDownList of Web and ListItem in Web
programming. The list control is populated with ListItems whose two
properties are DisplayText and Value. Very clear cut.

So I thought windows list controls would be similar, but noooo. I am
massively confused about the programming model. Please help.

Let me try to start some place.

CheckedListBox
===========
I populated it with a DataTable.

lctr.DisplayMember = "name";
lctr.ValueMember = "orgnizationID";
lctr.DataSource = myDataTable;

Now I attempt to read the selected value by doing the following.

int orgID = Convert.ToInt32(lctr.SelectedValue);

Well, it turns out that the object type of SelectedValue is DataRowView. So
what the heck is that about???

DataRowView rowView = (DataRowView)lctr.SelectedValue;
int orgID = Convert.ToInt32(rowView[0]);

Why can't I just do int orgID = Convert.ToInt32(lctr.SelectedValue) ??? Why
is DataRowView there? And besides, the only thing that is useful is the
first column of DataRowView.
Then I assumed that the display text must be in rowView[1]. Nope. It is a
string of zero lenght.

ListBox
=====

I populated it with a collection containing instances a class with has two
properties: OrganizationID, and OrganizationName. Typical.

lstSelectedOrgs.DataSource = null;
lstSelectedOrgs.ValueMember = "OrganizationID";
lstSelectedOrgs.DisplayMember = "OrganizationName";
lstSelectedOrgs.DataSource = reportSettings.ReportOrganizations;
//collection class that has instances of ReportOrganization

Now I have a button to remove a selected item from the list.

int orgID = Convert.ToInt32(lstSelectedOrgs.SelectedValue);
//remove it from the collection
reportSettings.ReportOrganizations.Remove(orgID);
//then call the method with the code above to repopulate the ListBox.

The problem here is, it works most of the time. Then the items list all
turn into "ReportOrganization" which is not class name of the object, not
what is supposed to be displayed!!! When I click on one of the items (which
all now say "ReportOrgnanizations", I get " Index was out of range. Must be
non-negative and less than the size of the collection."

And what is the difference between SelectedItem and SelectedValue? And what
exactly are in Items?
 
M

Morten Wennevik

Hi David,

See answers inline

I am used to working with DropDownList of Web and ListItem in Web
programming. The list control is populated with ListItems whose two
properties are DisplayText and Value. Very clear cut.

So I thought windows list controls would be similar, but noooo. I am
massively confused about the programming model. Please help.

Let me try to start some place.

CheckedListBox
===========
I populated it with a DataTable.

lctr.DisplayMember = "name";
lctr.ValueMember = "orgnizationID";
lctr.DataSource = myDataTable;

Now I attempt to read the selected value by doing the following.

int orgID = Convert.ToInt32(lctr.SelectedValue);

Well, it turns out that the object type of SelectedValue is DataRowView. So
what the heck is that about???

If the ValueMember isn't properly set it will return the object.ToString(). And since the ListControl is filled with DataRowViews this is what you get. Change

lctr.ValueMember = "orgnizationID";

to

lctr.ValueMember = "organizationID";
DataRowView rowView = (DataRowView)lctr.SelectedValue;
int orgID = Convert.ToInt32(rowView[0]);

Why can't I just do int orgID = Convert.ToInt32(lctr.SelectedValue) ??? Why
is DataRowView there? And besides, the only thing that is useful is the
first column of DataRowView.
Then I assumed that the display text must be in rowView[1]. Nope. It is a
string of zero lenght.

Yes, it is the type name of the selected object, see above.
ListBox
=====

I populated it with a collection containing instances a class with has two
properties: OrganizationID, and OrganizationName. Typical.

lstSelectedOrgs.DataSource = null;
lstSelectedOrgs.ValueMember = "OrganizationID";
lstSelectedOrgs.DisplayMember = "OrganizationName";
lstSelectedOrgs.DataSource = reportSettings.ReportOrganizations;
//collection class that has instances of ReportOrganization

Now I have a button to remove a selected item from the list.

int orgID = Convert.ToInt32(lstSelectedOrgs.SelectedValue);
//remove it from the collection
reportSettings.ReportOrganizations.Remove(orgID);
//then call the method with the code above to repopulate the ListBox.

The problem here is, it works most of the time. Then the items list all
turn into "ReportOrganization" which is not class name of the object, not
what is supposed to be displayed!!! When I click on one of the items (which
all now say "ReportOrgnanizations", I get " Index was out of range. Must be
non-negative and less than the size of the collection."

Ok, I'm not sure what is going on here.

And what is the difference between SelectedItem and SelectedValue? And what
exactly are in Items?

SelectedItem is the selected item in the list, the object.
SelectedValue is a named property of the SelectedItem, typically the column name in a datatable or a property in an object.

Items contains objects. In case of a DataTable, these objects are of type DataRowView.
 

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