adding items to listbox with extra data similar to databinding to dataview

  • Thread starter Thread starter TS
  • Start date Start date
T

TS

since you can't change items in a listbox that is databound, i have to add &
remove items using its items collection. The thing about this is that with
databinding, each item in the list can hold all the info for a particular
datarowview and can be accessed by converting the list item back to this
object.

The real thing i need to do is be able to have the list item's text
displayed correctly, then behind the scenes, i need to be able to access an
ID field from the data record associated with the item so that i can then do
an update to the db with the selected items of the list.

How do i save this extra data without databinding?

thanks
 
Note: i have a dual listbox and i need to first populate the left list box
from data.

The user can select an item from the left box and click button to move it to
right box, and the item needs to be removed from the left box. After the
user is satisfied, they hit submit, and I want to be able to do something
like the following:

Loop thru each selected item and get its value and update the corresponding
record in DB

foreach (object listitem in list_right.selecteditems)
{
//Ssave value to DB
SqlHelper.ExecuteNonQuery(Configuration.ConnectionString,
"spUpdateApplicantByDLN", listitem)
}

First of all i don't know how to add a value to a listitem, then i don't
know how to get it out later. Since i haven't seen how to do it anywhere i'm
guessing it can't be done.

So how do i accomplish what is need to ?

thanks again
 
Hi TS,

Thanks for your posting!!

I am not sure if your first post and the follow up reply point to the same
issue. For the seond issue, I think we may just loop through the selected
items collection of the left listbox, then add each selected item in the
right listbox, then, we may remove the selected item from the left listbox.
Sample code lists below:
for(int i=0;i<this.listBox1.SelectedItems.Count ;i++)
{
this.listBox2.Items.Add(this.listBox1.SelectedItems);
this.listBox1.Items.Remove(this.listBox1.SelectedItems);
}

For your first post issue, I am not sure I understand you well. Actually,
ListBox's value and display member concept only make sense for databinding
senario. If we add the items through code without databinding, there are
only items, without the concept of value member and display member. In
databinding senario, the listbox's value member usually binds to the ID key
field of datasource, while displaymember property usually binds to a string
display datacolumn. So, for selected items, we may just use the
SelectedValue property of ListBox to retrieve the associated ID value of
currently selected text item. So we should have no problem of retrieving
the ID field of underlying datasource.

Hope this helps, if I misunderstand you, please feel free to tell me, I
will work with you. Thanks
===========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top