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?
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?