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. Combo box bind to object ... need help

Combo box bind to object ... need help

Scheduled Pinned Locked Moved C#
help
5 Posts 3 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.
  • K Offline
    K Offline
    kakarato
    wrote on last edited by
    #1

    Can i just create a class or struct just for hold a set of static datas, let say objMonth contain two properties Month_Desc(String) and Month_ID (number). So Later when i just need to issue the command : cboMonth.DataSource = "objMonth". The combobox will be able to display Jan till Dec. Thanks.

    C 1 Reply Last reply
    0
    • K kakarato

      Can i just create a class or struct just for hold a set of static datas, let say objMonth contain two properties Month_Desc(String) and Month_ID (number). So Later when i just need to issue the command : cboMonth.DataSource = "objMonth". The combobox will be able to display Jan till Dec. Thanks.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The comboBox has a property called 'DisplayMember' which tells it which property on the object it binds to is the one to display. It needs to be a property from memory ( not a public variable ). Christian Graus - Microsoft MVP - C++

      K 1 Reply Last reply
      0
      • C Christian Graus

        The comboBox has a property called 'DisplayMember' which tells it which property on the object it binds to is the one to display. It needs to be a property from memory ( not a public variable ). Christian Graus - Microsoft MVP - C++

        K Offline
        K Offline
        kakarato
        wrote on last edited by
        #3

        I think my poor english make u misundestand. I've code below in my form ... private struct MonthList { public MonthList(string desc,string monthNo, string shortdesc) { this._desc = desc; this._number = monthNo; this._shortdesc = shortdesc; } private string _desc; private string _number; private string _shortdesc; public string monthDesc { get {return _desc;} } public string monthNo { get {return _desc;} } public string shortDesc { get {return _shortdesc; } } } private void init_cboMonth() { ArrayList month = new ArrayList (); month.Add( new MonthList ("January","1","Jan")); month.Add( new MonthList ("Feburay","2","Feb")); month.Add( new MonthList ("March","3","Mar")); month.Add( new MonthList ("April","4","Apl")); month.Add( new MonthList ("May","5","May")); month.Add( new MonthList ("June","6","Jun")); month.Add( new MonthList ("July","7","Jly")); month.Add( new MonthList ("August","8","Aug")); month.Add( new MonthList ("September","9","Sep")); month.Add( new MonthList ("Otocber","10","Oct")); month.Add( new MonthList ("November","11","Nov")); month.Add( new MonthList ("December","12","Dec")); this.comboMonth.DataSource = month; this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; } It work fine in the form.. by when i try to seperate code above it to another file like below public class stdData { public static ArrayList getMonthList() { ArrayList month = new ArrayList (); month.Add( new MonthList ("January","1","Jan")); month.Add( new MonthList ("Feburay","2","Feb")); month.Add( new MonthList ("March","3","Mar")); month.Add( new MonthList ("April","4","Apl")); month.Add( new MonthList ("May","5","May")); month.Add( new MonthList ("June","6","Jun")); month.Add( new MonthList ("July","7","Jly")); month.Add( new MonthList ("August","8","Aug")); month.Add( new MonthList ("September","9","Sep")); month.Add( new MonthList ("Otocber","10","Oct")); month.Add( new MonthList ("November","11","Nov")); month.Add( new MonthList ("December","12","Dec")); return month; } } private struct MonthList { public MonthList(string desc,string monthNo, string shortdesc) { this._desc = desc; this._number = monthNo; this._shortdesc = shortdesc; } private string _desc; private string _number; private string _shortdesc; public string monthDesc ... public string monthNo ... public strin

        J 1 Reply Last reply
        0
        • K kakarato

          I think my poor english make u misundestand. I've code below in my form ... private struct MonthList { public MonthList(string desc,string monthNo, string shortdesc) { this._desc = desc; this._number = monthNo; this._shortdesc = shortdesc; } private string _desc; private string _number; private string _shortdesc; public string monthDesc { get {return _desc;} } public string monthNo { get {return _desc;} } public string shortDesc { get {return _shortdesc; } } } private void init_cboMonth() { ArrayList month = new ArrayList (); month.Add( new MonthList ("January","1","Jan")); month.Add( new MonthList ("Feburay","2","Feb")); month.Add( new MonthList ("March","3","Mar")); month.Add( new MonthList ("April","4","Apl")); month.Add( new MonthList ("May","5","May")); month.Add( new MonthList ("June","6","Jun")); month.Add( new MonthList ("July","7","Jly")); month.Add( new MonthList ("August","8","Aug")); month.Add( new MonthList ("September","9","Sep")); month.Add( new MonthList ("Otocber","10","Oct")); month.Add( new MonthList ("November","11","Nov")); month.Add( new MonthList ("December","12","Dec")); this.comboMonth.DataSource = month; this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; } It work fine in the form.. by when i try to seperate code above it to another file like below public class stdData { public static ArrayList getMonthList() { ArrayList month = new ArrayList (); month.Add( new MonthList ("January","1","Jan")); month.Add( new MonthList ("Feburay","2","Feb")); month.Add( new MonthList ("March","3","Mar")); month.Add( new MonthList ("April","4","Apl")); month.Add( new MonthList ("May","5","May")); month.Add( new MonthList ("June","6","Jun")); month.Add( new MonthList ("July","7","Jly")); month.Add( new MonthList ("August","8","Aug")); month.Add( new MonthList ("September","9","Sep")); month.Add( new MonthList ("Otocber","10","Oct")); month.Add( new MonthList ("November","11","Nov")); month.Add( new MonthList ("December","12","Dec")); return month; } } private struct MonthList { public MonthList(string desc,string monthNo, string shortdesc) { this._desc = desc; this._number = monthNo; this._shortdesc = shortdesc; } private string _desc; private string _number; private string _shortdesc; public string monthDesc ... public string monthNo ... public strin

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          kakarato wrote: So what is goinf wrong Simple, you defined getMonthList() as static, but then tried to call it on an instance stdData stdData = new stdData(); ArrayList month = stdData.getMonthList(); this.comboMonth.DataSource = month; this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; //this line will cause error try this instead this.comboMonth.DataSource = stdData.getMonthList(); this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; //this line will cause error

          K 1 Reply Last reply
          0
          • J J4amieC

            kakarato wrote: So what is goinf wrong Simple, you defined getMonthList() as static, but then tried to call it on an instance stdData stdData = new stdData(); ArrayList month = stdData.getMonthList(); this.comboMonth.DataSource = month; this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; //this line will cause error try this instead this.comboMonth.DataSource = stdData.getMonthList(); this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; //this line will cause error

            K Offline
            K Offline
            kakarato
            wrote on last edited by
            #5

            Thanks for help but an Error Message come out at the same line. The error message is : "Could not bind to the new display member." If i take out the line "this.comboMonth.ValueMember="monthNo" something strange is display in the combo box .. In the combox box it will display "XYZ.Model.Monthlist", where the XYZ.Model is the namespace. this.comboMonth.DataSource = stdData.getMonthList(); this.comboMonth.DisplayMember = "monthDesc"; //If i remark line below some thing strange come out in the combo box //this.comboMonth.ValueMember = "monthNo";

            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