vargasbo
asked on
Checking Items in CheckListBox
Greetings,
Here's the issue. I first have users check certain items in the checklistBox, and I save thier selection in an arraylist. I store all the items in the checklistBox which should also store whichs one are checked. The problem comes when i want to repopulate the same checkListBox later from the items stored in the arraylist.
Here's the code to populate the ChecklistBox...
StreamReader myXmlStream = new StreamReader(myFileStream) ;
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(myXmlStr eam);
chBook.DataSource = myDataSet.Tables[4];
chBook.DisplayMember = myDataSet.Tables[4].Column s[0].ToStr ing();
chBook.ValueMember = myDataSet.Tables[4].Column s[1].ToStr ing();
Here's how i store the information...
for(int i = 0; i < this.chBook.Items.Count; i++){
ls.books.Add(chBook.Items[ i]);}
Now how do i repopulate the CheckListBox from the arraylist? I've tried so many things I won't even post them here.
Thanks,
Here's the issue. I first have users check certain items in the checklistBox, and I save thier selection in an arraylist. I store all the items in the checklistBox which should also store whichs one are checked. The problem comes when i want to repopulate the same checkListBox later from the items stored in the arraylist.
Here's the code to populate the ChecklistBox...
StreamReader myXmlStream = new StreamReader(myFileStream)
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(myXmlStr
chBook.DataSource = myDataSet.Tables[4];
chBook.DisplayMember = myDataSet.Tables[4].Column
chBook.ValueMember = myDataSet.Tables[4].Column
Here's how i store the information...
for(int i = 0; i < this.chBook.Items.Count; i++){
ls.books.Add(chBook.Items[
Now how do i repopulate the CheckListBox from the arraylist? I've tried so many things I won't even post them here.
Thanks,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
aacool
What i did try your following suggestion, but it didn't work. What shows up in the CheckedListBox is System.DataView..
DataRowView []oCheckedItems= new DataRowView[ls.books.Count ];
ls.books.CopyTo(oCheckedIt ems,0);
chBook.DataSource = null; //Have to remove Datasource otherwise can't edit items
chBook.Items.AddRange(ls.b ooks.ToArr ay());
Why can't i just save the items in the list with it's checked status? This is the code I'm using which shows the item name, but it doesn't show it's checked state.
for(int x = 0; x < oCheckedItems.Length; x++) {
chGrade.Items.Add(oChecked Items[x].R ow[0]);}
What i did try your following suggestion, but it didn't work. What shows up in the CheckedListBox is System.DataView..
DataRowView []oCheckedItems= new DataRowView[ls.books.Count
ls.books.CopyTo(oCheckedIt
chBook.DataSource = null; //Have to remove Datasource otherwise can't edit items
chBook.Items.AddRange(ls.b
Why can't i just save the items in the list with it's checked status? This is the code I'm using which shows the item name, but it doesn't show it's checked state.
for(int x = 0; x < oCheckedItems.Length; x++) {
chGrade.Items.Add(oChecked
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
for(int i=0;i<chBook.Items.Count;i ++)
{
CheckedItem chItem = new CheckedItem();
chItem.text = chBook.Items[i].Text; <---- Can't do this
(chBook.Items[i].CheckStat e=CheckSta te.Checked ) ? chItem.itemState=true : chItem.itemState = false; <<< --- or this
chItemsArray[i++] = chItem;
chItem.Dispose();
}
Have to uses DataRowView b/c the CheckListBox is tied to a Datasource, That why i can't uses the chBook.Items[i].Text.
{
CheckedItem chItem = new CheckedItem();
chItem.text = chBook.Items[i].Text; <---- Can't do this
(chBook.Items[i].CheckStat
chItemsArray[i++] = chItem;
chItem.Dispose();
}
Have to uses DataRowView b/c the CheckListBox is tied to a Datasource, That why i can't uses the chBook.Items[i].Text.
ASKER
OK, I finally got the code to work and here's the code
When Items first saves....
for(int i = 0; i < this.chBook.Items.Count; i++){
CheckedItems chItem = new CheckedItems();
chItem.text = ((DataRowView)chBook.Items [i]).Row[0 ].ToString ();
chItem.check = chBook.GetItemChecked(i);
ls.books.Add(chItem);
chItem = null;
}
When repopulating the form
chBook.DataSource = null;
for(int x = 0; x < ls.books.Count; x++) {
if(((CheckedItems)ls.books [x]).check == true)
chBook.Items.Add(((Checked Items)ls.b ooks[x]).t ext,true);
else
chBook.Items.Add(((Checked Items)ls.b ooks[x]).t ext,false) ;
}
Thanks for the help
When Items first saves....
for(int i = 0; i < this.chBook.Items.Count; i++){
CheckedItems chItem = new CheckedItems();
chItem.text = ((DataRowView)chBook.Items
chItem.check = chBook.GetItemChecked(i);
ls.books.Add(chItem);
chItem = null;
}
When repopulating the form
chBook.DataSource = null;
for(int x = 0; x < ls.books.Count; x++) {
if(((CheckedItems)ls.books
chBook.Items.Add(((Checked
else
chBook.Items.Add(((Checked
}
Thanks for the help
ArrayList lsBooks;
CheckedListBox chBook
private void setCBValues()
{
chBook.Items.Clear();
// Populate checkedListBox with items from array
chBook.Items.AddRange(lsBo
}
I have a fully working example to send you if you need it - please mail me at aacool - hotmail.com if needed