Asp.Net Array List DataBinding and Sorting

This article is written by Pon Saravanan  on 15-Jun-10 Last modified on :15-Jun-10

DataBinding GridView with Array List

DataBinding is an integral part in developing GUI intensive applications. It really eases the job of populating the data into the control from respective DataSource. The DataSource is usually being an IEnumerator. Means all the objects that implement an IEnumerator can be used to bind the controls.
GridView DataBinding
Though Binding a GridView is very simple task in VB.Net, the following is essential. Understanding
• how to use a DataSource
• Once the DataSource is given how to bind the data.
Why ArrayList over Array?
The reason behind I have chosen ArrayList over array is, because of the ease of use. The array needs to be re dim every time when the number of elements is changed. If you want to add a new item then you have to re dim the array.
But ArrayList are much easier to work with. The reason behind is, it is something like collection in Classic VB. You can add, remove the items easily. There is absolutely no need to worry about the count of items. As a new item will be added, removed and the count will be calculated automatically
Sorting GridView
Since the GridView natively supports the sorting, I tried to sort the ArrayList and rebind again. This is really much easier. Call the sort method of the ArrayList and assign the DataSource again and call the DataBind. That’s it the GridView will be sorted.

Source Code

Mark up (Aspx)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:GridView runat="server" ID="GridView1" AllowSorting="true">

Code Behind

Partial Class _Default
    Inherits System.Web.UI.Page
    Private Property ArrayDataSource() As ArrayList
            Return DirectCast(ViewState("ArrayDataSource"), ArrayList)
        End Get
        Set(ByVal value As ArrayList)
            ViewState("ArrayDataSource") = value
        End Set
    End Property
    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            ArrayDataSource = GetArrayList()
        End If
    End Sub
    Private Function GetArrayList() As ArrayList
        Dim Output As New ArrayList
        Dim RandomNumbers As New Random()
        For RowIndex = 1 To 10
            Output.Add(RandomNumbers.Next(1, 50))
        Return Output
    End Function
    Protected Sub GridView1_Sorting(ByVal sender As Object, _
                ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting
    End Sub
    Private Sub LoadData()
        GridView1.DataSource = ArrayDataSource
    End Sub
End Class


Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    

Spam Bot Trap


Select Theme