Combo box bind to object ... need help
-
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.
-
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.
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++
-
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++
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 belowpublic 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
-
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 belowpublic 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
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 insteadthis.comboMonth.DataSource = stdData.getMonthList(); this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; //this line will cause error
-
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 insteadthis.comboMonth.DataSource = stdData.getMonthList(); this.comboMonth.DisplayMember = "monthDesc"; this.comboMonth.ValueMember = "monthNo"; //this line will cause error
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";