Thursday 18 March 2010

Different ways of binding data to a drop down list

Different ways to bind data to a drop down list

* Binding to a Simple Array
* Binding to an Enum
* Binding to an ArrayList
* Binding to a hashtable
* Binding to a data table
* Binding to a data set
* Binding to a data view
* Binding to a set of objects
* Binding manually

Binding to a Simple Array

Dim months() As String = {"January", "February", "March", "April", "May", "June", "July"}
Me.DropDownList1.DataSource = months
Me.DropDownList1.DataBind()

Here both the “text” and “value” of the selected item of the “dropdownlist” would be same.

Binding to an Enum

public enum Color
{
RED,
GREEN,
BLUE
}

To bind "Color" to dropdownlist ddlColor

ddlColor.DataSource = Enum.GetNames(typeof(Color));
ddlColor.DataBind();

and to get the selected value

Color selectedColor = (Color)Enum.Parse(ddlColor.SelectedValue);

Binding to an ArrayList

ArrayList has lot of flexibilities that a simple array does not have. For example, we can remove, clear or do any manipulations within the “ArrayList”.

Dim ar As New ArrayList
ar.Add("January")
ar.Add("February")
ar.Add("March")

Me.DropDownList1.DataSource = ar
Me.DropDownList1.DataBind()

The “text” and “value” of any selected item within the “dropdownlist” would still be the same.

Binding to a hashtable

Hashtable is very similar to an “ArrayList.” but it stores information as key-value pair.

Dim months As New Hashtable
ht.Add("1001", "Jan")
ht.Add("1002", "Feb")

Me.DropDownList1.DataSource = months
Me.DropDownList1.DataTextField = "value"
Me.DropDownList1.DataValueField = "key"
Me.DropDownList1.DataBind()

Binding to a DataTable or DataSet or DataView

Me.DropDownList1.DataSource = DataTable / DataSet.Tables(index) / DataView
Me.DropDownList1.DataTextField = "ProductName"
Me.DropDownList1.DataValueField = "ProductID"
Me.DropDownList1.DataBind()

Binding to a set of objects

If you have your own structure (defined in the form of “class” or “structure”), you can still use them to create several objects and together bind to the “dropdownlist.”

At this moment, I just created a simple class (CStudent) which can hold a “regdno” and “sname”. I also defined two read/write properties to “set-get” the property values. Let us go through the class first.

Public Class Student

Private _regdno As String
Private _sname As String

Public Sub New(ByVal r As String, ByVal n As String)
RegNo = r
SName = n
End Sub
Public Property RegNo() As String
Get
Return _regdno
End Get
Set(ByVal Value As String)
_regdno = Value
End Set
End Property

Public Property SName() As String
Get
Return _sname
End Get
Set(ByVal Value As String)
_sname = Value
End Set
End Property

End Class

Once you create the above class, we need to declare objects (along with values) to work with those classes. Make sure that a “class” is simply a specification (or user-defined data type). It does not allocate any memory to hold the values (unless you create the objects). The following code creates a few objects based on the class “CStudent” and binds it to the “dropdownlist.”


Me.DropDownList1.Items.Clear()

Dim items As New ArrayList

items.Add(New Student("1001", "test1"))
items.Add(New Student("1002", "test2"))
items.Add(New Student("1003", "test3"))

Me.DropDownList1.DataSource = items
Me.DropDownList1.DataTextField = "SName"
Me.DropDownList1.DataValueField = "RegNo"
Me.DropDownList1.DataBind()


“ArrayList” can hold any type of object.

Binding Manually

We can manually add different types of items to the “dropdownlist.”

Dim li As ListItem
li = New ListItem
li.Text = "first"
li.Value = 1
Me.DropDownList1.Items.Add(li)

li = New ListItem("second")
Me.DropDownList1.Items.Add(li)

li = New ListItem("third", "3")
Me.DropDownList1.Items.Add(li)

For i As Integer = 4 To 10
Me.DropDownList1.Items.Add(New ListItem("item" & i, i))
Next

No comments:

Post a Comment