Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListBox and DataSource

ListBox and DataSource

Scheduled Pinned Locked Moved C#
questionhelpannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mkomasi
    wrote on last edited by
    #1

    Hi, I have two lists (A and B).I want to add some items from A to B and remove the items from A. I've used two ListBoxes for this situation. I want to display Name of items and for adding and removing I have to know ID of each item. So I use ListBox.DataSource to fill items. First I define a class: public class USState { private string myShortName ; private string myLongName ; public USState(string strLongName, string strShortName) { this.myShortName = strShortName; this.myLongName = strLongName; } public string ShortName { get { return myShortName; } } public string LongName { get { return myLongName ; } } public override string ToString() { return this.ShortName + " - " + this.LongName; } } Then for filling list items : ArrayList USStatesA = new ArrayList() ; ArrayList USStatesB = new ArrayList() ; USStatesA.Add(new USState("Alabama", "AL")); USStatesA.Add(new USState("Washington", "WA")) ; USStatesA.Add(new USState("West Virginia", "WV")); USStatesA.Add(new USState("Wisconsin", "WI")) ; USStatesB.Add(new USState("Wyoming", "WY")); ListBoxA.DisplayMember = "LongName" ; ListBoxA.ValueMember = "ShortName" ; ListBoxA.DataSource = USStatesA ; ListBoxB.DisplayMember = "LongName" ; ListBoxB.ValueMember = "ShortName" ; ListBoxB.DataSource = USStatesB ; Everything is OK by above codes. But the problem is: When I Remove an object from ListBoxA and then Add it to ListBoxB; no changes will be appear in ListBoxA.Items or ListBoxB.Items . How can I update ListBox Items same as its DataSource updated? regards, Mehdi

    C 1 Reply Last reply
    0
    • M mkomasi

      Hi, I have two lists (A and B).I want to add some items from A to B and remove the items from A. I've used two ListBoxes for this situation. I want to display Name of items and for adding and removing I have to know ID of each item. So I use ListBox.DataSource to fill items. First I define a class: public class USState { private string myShortName ; private string myLongName ; public USState(string strLongName, string strShortName) { this.myShortName = strShortName; this.myLongName = strLongName; } public string ShortName { get { return myShortName; } } public string LongName { get { return myLongName ; } } public override string ToString() { return this.ShortName + " - " + this.LongName; } } Then for filling list items : ArrayList USStatesA = new ArrayList() ; ArrayList USStatesB = new ArrayList() ; USStatesA.Add(new USState("Alabama", "AL")); USStatesA.Add(new USState("Washington", "WA")) ; USStatesA.Add(new USState("West Virginia", "WV")); USStatesA.Add(new USState("Wisconsin", "WI")) ; USStatesB.Add(new USState("Wyoming", "WY")); ListBoxA.DisplayMember = "LongName" ; ListBoxA.ValueMember = "ShortName" ; ListBoxA.DataSource = USStatesA ; ListBoxB.DisplayMember = "LongName" ; ListBoxB.ValueMember = "ShortName" ; ListBoxB.DataSource = USStatesB ; Everything is OK by above codes. But the problem is: When I Remove an object from ListBoxA and then Add it to ListBoxB; no changes will be appear in ListBoxA.Items or ListBoxB.Items . How can I update ListBox Items same as its DataSource updated? regards, Mehdi

      C Offline
      C Offline
      Chris Jobson
      wrote on last edited by
      #2

      One method that will work is to invoke the Refresh method of the Currencymanager objects involved, e.g. after you've moved an object from one ArrayList to the other: CurrencyManager cm = BindingContext[USStatesA] as CurrencyManager; if (cm != null) cm.Refresh(); cm = BindingContext[USStatesB] as CurrencyManager; if (cm != null) cm.Refresh(); A nicer method might be to wrap the ArrayLists (or derive from them) in an object of your own which implements the IBindingList interface. This allows an event to be raised when the list is changed, and I assume that the CurrencyManager traps this event and automatically updates the bound controls (but this is based only on my understanding of the documentation - I haven't tried it :~ ). Chris Jobson

      M 1 Reply Last reply
      0
      • C Chris Jobson

        One method that will work is to invoke the Refresh method of the Currencymanager objects involved, e.g. after you've moved an object from one ArrayList to the other: CurrencyManager cm = BindingContext[USStatesA] as CurrencyManager; if (cm != null) cm.Refresh(); cm = BindingContext[USStatesB] as CurrencyManager; if (cm != null) cm.Refresh(); A nicer method might be to wrap the ArrayLists (or derive from them) in an object of your own which implements the IBindingList interface. This allows an event to be raised when the list is changed, and I assume that the CurrencyManager traps this event and automatically updates the bound controls (but this is based only on my understanding of the documentation - I haven't tried it :~ ). Chris Jobson

        M Offline
        M Offline
        mkomasi
        wrote on last edited by
        #3

        Thanks Chris. I think I didn't write the problem clearly. I tell you it again. I write below method to remove an item from ListBoxA and add it to ListBoxB: void TransferItem() { object SelectedObject = ListBoxA.SelectedItem; ((ArrayList)ListBoxA.DataSource).Remove(SelectedObject); ((ArrayList)ListBoxB.DataSource).Add(SelectedObject); } By this method the DataSource of ListBoxA and ListBoxB has been changed, but no changes will be appear in ListBoxA.Items and ListBoxB.Items . I couldn't change the Items directly because of using DataSource for ListBoxes. Can you help me please. Mehdi

        C 1 Reply Last reply
        0
        • M mkomasi

          Thanks Chris. I think I didn't write the problem clearly. I tell you it again. I write below method to remove an item from ListBoxA and add it to ListBoxB: void TransferItem() { object SelectedObject = ListBoxA.SelectedItem; ((ArrayList)ListBoxA.DataSource).Remove(SelectedObject); ((ArrayList)ListBoxB.DataSource).Add(SelectedObject); } By this method the DataSource of ListBoxA and ListBoxB has been changed, but no changes will be appear in ListBoxA.Items and ListBoxB.Items . I couldn't change the Items directly because of using DataSource for ListBoxes. Can you help me please. Mehdi

          C Offline
          C Offline
          Chris Jobson
          wrote on last edited by
          #4

          Mehdi, If you add the code I gave, or the following modified version of it, at the end of the TransferItem method in your previous posting it should work: CurrencyManager cm = BindingContext[ListBoxA.DataSource] as CurrencyManager; if (cm != null) cm.Refresh(); cm = BindingContext[ListBoxB.DataSource] as CurrencyManager; if (cm != null) cm.Refresh(); Chris Jobson

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups