welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Saturday, August 6, 2011

Sorting in datatable this article i will explain about do sorting in datatable.

Intially this type of situation comes after bind the data to grid view or datalist ,if you want
do sorting link ascending or descending order that time again u need to fetch data from the database.Instead of you can follow this method sorting in datatable..

Before bind the data to gridview or datalist.make one copy in session..Then when do sorting use the session data do sorting in datatable and bind gridview again..

Steps to follows...
For an example you have dropdownlist and gridview..
In dropdown list added ascending and descendin..

After bind the data in gridview..By using dropdown we sorting...

Write the below code in dropdown list selected index change...

Dim dt1 as new datable
 dt1 = Session("dtitems") ' Here i copy the data form session to datatable

"Here copied the data to datatable

        Dim dataview As DataView = dt1.DefaultView

        If ddldescen.SelectedIndex = 1 Then
            dataview.Sort = "Number desc"  'Number is column name in datatable,So change the column in descending order in datatable
        ElseIf ddlascend.SelectedIndex = 2 Then
            dataview.Sort = "Number ASC"  'Number is column name in datatable,So change the column in ascending order in datatable
         End If

        DataList1.DataSource = dataview 'Finally i bind the data in datalist

i hope this one helpful to all.....
Post your valuable comments here.......


Post a Comment