| |||||||||
| View Poll Results: How do you feel about this program? | |||
| Very good | | 0 | 0% |
| Good | | 0 | 0% |
| Average | | 0 | 0% |
| Voters: 0. You may not vote on this poll | |||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
hi,
Here, I am going to explain how to bind array variables or database tables to a combobox in .NET application. 1. Array to combo box ================= The following line of codes will return the values from an array to combo box. ~~~~~~~~ dim Days() as String = {"SUN","MON0","TUE","WED","THU","FRI","SAT"} combobox1.DataSource=Days ~~~~~~ 2. Array List to combo box ==================== The following line of codes will return the values from an arraylist to combo box ~~~~~~~~~ dim Months as new ArrayList 'add month structure entries to the arraylist with Months .Add("JANUARY") .Add("FEBRUARY") .Add("MARCH") .Add("APRIL") .Add("MAY") .Add("JUNE") .Add("JULY") .Add("AUGUST") .Add("SEPTEMBER") .Add("OCTOBER") .Add("NOVEMBER") .Add("DECEMBER") End With combobox1.DataSource=Days ~~~~~~~~~~ 3. Taking key value from combo box using an arraylist ======================================== The following line of codes will return the key value from an arraylist to combo box ~~~~~~~~~ 'define a structure for Year which has ' a year and id. protected structure Months private MonthName as String private MonthId as Integer public sub new(monname as String, monid as Integer) divName=monname divId = monid End Sub public readonly property getMonName() as String get return MonthName End Get end property public readonly property getMonId() as Integer get return MonthID End Get End Property End structure dim MyMonths as new ArrayList 'add month structure entries to the arraylist with MyMonths .Add(new Years("JANUARY",1)) .Add(new Years("FEBRUARY",2)) .Add(new Years("MARCH",3)) .Add(new Years("APRIL",4)) .Add(new Years("MAY",5)) .Add(new Years("JUNE",6)) .Add(new Years("JULY",7)) .Add(new Years("AUGUST",8)) .Add(new Years("SEPTEMBER",9)) .Add(new Years("OCTOBER",10)) .Add(new Years("NOVEMBER",11)) .Add(new Years("DECEMBER",12)) End With 'bind the arraylist to the combo box With combobox1 .datasource=MyMonths .Displaymember="getMonName" .ValueMember="getMonId" End With ~~~~~~~~~~ 3. Taking key value from combo box using an database ======================================== The following line of codes will return the key value from a database table to combo box ~~~~~~~~~ 'bind the arraylist to the combo box With combobox1 .Datasource=dsproducts.Tables("MONTHS") .Displaymember="MonthName" .ValueMember="MonthID" End With ~~~~~~~~ PS : Similarly you can populated into combo using dataView (Protected dvMonths as DataView) thanks Benoy binoy_rb@hotmail.com |
|
#2
|
||||
|
||||
|
Actually, .NET has built in ways to get the days and months, both names and values.
So, although you code seems correct (I haven't tried it), there is an easier way to do it. |
|
#3
|
|||
|
|||
|
Hai,
This question is related to VB .NET. I have a form that gets/stores customer address. I am getting data from dataset and write into in. I created a structure for US states Public Structure State Private shortName As String Private longName As String Public Sub New(ByVal lname As String, ByVal sname As String) shortName = sname longName = lname End Sub Public Property getShortName() As String Get Return shortName End Get Set(ByVal Value As String) End Set End Property Public Property getLongName() As String Get Return longName End Get Set(ByVal Value As String) End Set End Property End Structure ' US states array Public USstates As State() = New State() { _ New State("Alabama", "AL") _ , New State("Alaska", "AK") _ , New State("Arizona", "AZ") _ , New State("Arkansas", "AR") _ , New State("California", "CA"), etc } How do I bind the US states array to the Combo box and to the customerAddress table? The combo box should display long names such as "Arizona", etc. When user selects "Arizona", the short form "AZ" should get saved into the customer table. How could I bind it? |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Data binding to a combo box in .NET or array management with Combo box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|