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 Asp.net 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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView runat="server" ID="GridView1" AllowSorting="true">
    </asp:GridView>
    </div>
    </form>
</body>
</html>

Code Behind

Partial Class _Default
    Inherits System.Web.UI.Page
    Private Property ArrayDataSource() As ArrayList
        Get
            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()
            LoadData()
        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))
        Next
        Return Output
    End Function
    Protected Sub GridView1_Sorting(ByVal sender As Object, _
                ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting
        ArrayDataSource.Sort()
        LoadData()
    End Sub
    Private Sub LoadData()
        GridView1.DataSource = ArrayDataSource
        GridView1.DataBind()
    End Sub
End Class











Comments

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



Spam Bot Trap



   



Select Theme
White
Blue
Brown
Gray