SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


ASP.NET, SortedList Object and sort order in DropDownList Controls


The SortedList object which has the class definition System.Collections.SortedList has the properties and features of the ArrayList and the Hashtable objects. As well as the other objects which the SortedList object is compared to, the SortedList object contains items in key-value pairs too.
The SortedList object automatically sorts the items added to its collection by using the Add() method in alphabetic or numeric order according to the key values.

Items are added to the SortedList with the Add() method. Add method adds an element with the specified key and value to the System.Collections.SortedList

Sub Add(ByVal key As Object, ByVal value As Object)
key: The key of the element to add.
value: The value of the element to add. The value can be null.





Private Sub PrepareSortedList()

Dim slSortedList = New SortedList
slSortedList.Add(3, "DataSet")
slSortedList.Add(2, "HashTable")
slSortedList.Add(1, "SortedList")

DropDownList2.DataSource = slSortedList
DropDownList2.DataTextField = "Value"
DropDownList2.DataValueField = "Key"

DropDownList2.DataBind()

End Sub

This code causes the below DropDownList2

<select name="DropDownList2" id="DropDownList2" style="width:264px;Z-INDEX: 102; LEFT: 88px; POSITION: absolute; TOP: 48px">

<option value="1">SortedList</option>
<option value="2">HashTable</option>
<option value="3">DataSet</option>

</select>

Ordering the key-value pairs may not be a problem unless you want to display the items in an alphabetic order in the dropdownlist. You may update your stored procedure or select statement in order to get the items ordered as desired by using the order by clause. But if you change your code a little

Private Sub PrepareSortedListByAlphabetic()

Dim slSortedList = New SortedList
slSortedList.Add("DataSet", 3)
slSortedList.Add("HashTable", 2)
slSortedList.Add("SortedList", 1)

DropDownList2.DataSource = slSortedList
DropDownList2.DataTextField = "Key"
DropDownList2.DataValueField = "Value"

DropDownList2.DataBind()

End Sub

You will have alphabetically sorted items in the dropdownlist as below.

<select name="DropDownList2" id="DropDownList2" style="width:264px;Z-INDEX: 102; LEFT: 88px; POSITION: absolute; TOP: 48px">
<option value="3">DataSet</option>
<option value="2">HashTable</option>
<option value="1">SortedList</option>
</select>

An other example if you have a datareader and wish to create a SortedList object by populating the sortedlist with the datareader, you can use the sample code below:
We have a datareader object as the data source and a new SortedList object.

Dim drDataReader As SqlClient.SqlDataReader
Dim slSortedList As New SortedList

We have some variables to illustrate how we can use the SortedList object where sorting the data is somehow complex and can not be managed on the data access layer using the order by clause in the stored procedure
Dim PartId As Int32
Dim PartCode As String
Dim PartDescription As String

Fill the drDataReader datareader object

drDataReader = .ListBriefByCustomerIdDR(customerId)

Dim sqlConnStr As String = "workstation id=kodyaz;packet size=4096;user=eralper;password=pwd;data source=""."";persist security info=False;initial catalog=KodyazDB"
Dim sqlConn As New SqlClient.SqlConnection(sqlConnStr)

Dim sqlSelectCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT PartId, PartCode, PartDescription FROM tblSpareParts (NoLock)")
sqlSelectCommand.Connection = sqlConn

sqlConn.Open()
drDataReader = sqlSelectCommand.ExecuteReader(CommandBehavior.CloseConnection)

Put together part code and part description fields into one field which will be displayed on a dropdownlist and save the text field and the value field into a sortedlist object which will sort the inserted items automatically in alphabetic or numeric order according to the key value. Key value is the first argument of the Add method of the SortedList object.

While drDataReader.Read
    PartId = CType(drDataReader.Item("PartId"), Int32)
    PartCode = drDataReader.Item("PartCode").ToString
    PartDescription = drDataReader.Item("PartDescription").ToString

    If PartCode.Length = 0 Then
        slSortedList.Add(drDataReader.Item("Brand").ToString & "-" & drDataReader.Item("PartType").ToString, PartId)
    Else
        slSortedList.Add(PartCode & "-" & PartDescription, PartId)
    End If
End While

Note that the key value is alphanumeric in the above sample, the id field is inserted to the SortedList object as the value of the key-value pair. This is a simple trick to get the items sorted by their descriptions.

Now we can bind the data which is contained within the SortedList object as below,

dropdownlistParts.DataValueField = "Value"
dropdownlistParts.DataTextField = "Key"
dropdownlistParts.DataSource = slSortedList
dropdownlistParts.DataBind()

Now you will see that the items will be listed in the dropdownlist object in the alphabetic order.

The above sample codes for SortedList object illustrates how databinding can be done between SortedList object and dropdownlist page control.

Here is a sample for CheckBoxList control and databinding for CheckBoxList control with SortedList object.

Private Sub BindCheckBoxList()

Dim slSortedList = New SortedList
slSortedList.Add(3, "DataSet")
slSortedList.Add(2, "HashTable")
slSortedList.Add(1, "SortedList")

CheckBoxList1.DataSource = slSortedList
CheckBoxList1.DataTextField = "Value"
CheckBoxList1.DataValueField = "Key"

CheckBoxList1.DataBind()

End Sub

And you can retrieve the value selected in the checkboxlist by using the following code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim i As Int16 = 0
    While i < CheckBoxList1.Items.Count
        If CheckBoxList1.Items(i).Selected Then
            Label1.Text &= CheckBoxList1.Items(i).Value & " , "
        End If
        i = i + 1
    End While
End Sub

Run a similar sample for a RadioButtonList

Private Sub BindRadioButtonList()

Dim slSortedList = New SortedList
slSortedList.Add(3, "DataSet")
slSortedList.Add(2, "HashTable")
slSortedList.Add(1, "SortedList")

RadioButtonList1.DataSource = slSortedList
RadioButtonList1.DataTextField = "Value"
RadioButtonList1.DataValueField = "Key"

RadioButtonList1.DataBind()

End Sub

And you can read the value by the value of RadioButtonList1.SelectedValue property

If you are using a ListBox on your web form, you can bind SortedList data to ListBox1 by the help of the following sub procedure

Private Sub BindListBox()

Dim slSortedList = New SortedList
slSortedList.Add(3, "DataSet")
slSortedList.Add(2, "HashTable")
slSortedList.Add(1, "SortedList")

ListBox1.DataSource = slSortedList
ListBox1.DataTextField = "Value"
ListBox1.DataValueField = "Key"

ListBox1.DataBind()

End Sub

Here is the procedure which you can get the values selected on the ListBox


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If ListBox1.SelectionMode = ListSelectionMode.Single Then
    Label2.Text = ListBox1.SelectedValue
ElseIf ListBox1.SelectionMode = ListSelectionMode.Multiple Then
    Dim j As Int16 = 0
    While j < ListBox1.Items.Count
        If ListBox1.Items(j).Selected Then
            Label2.Text &= ListBox1.Items(j).Value & " , "
        End If
        j = j + 1
    End While
End If

End Sub



Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.