Thursday, January 14, 2010

Multiple column sort of a gridview in asp.net

Most commercial applications require sorting to be integral to any gridview. You can implement multiple column sort using these instructions.


1. Add a gridview to the aspx file.

An image has been added as blog security features do not display the above code properly.

2. Include a proceudre to bind data to the gridview.

Protected Sub BindGrid()
Dim x As New Business.Employees
Dim tb As New DataTable
tb = x.GetTestTable.Tables(0)
Dim dv As DataView
dv = tb.DefaultView
If Not (GridView1.Attributes("SortExpression") Is Nothing) Then
dv.Sort = GridView1.Attributes("SortExpression")
End If
GridView1.DataSource = dv
GridView1.DataBind()
End Sub


3. Include a procedure to be called when the user clicks on the column header


Private Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim s As String
'Get existing sort expression
s = GridView1.Attributes("SortExpression")

'If the column has been included
If InStr(s, e.SortExpression.ToString()) > 0 Then
'If the column has been sorted in descending order, we remove the column from the sort expression
If InStr(s, e.SortExpression.ToString() & " DESC") > 0 Then
s = s.Replace(e.SortExpression.ToString() & " DESC", "")
s = LTrim(RTrim(s))
If s.IndexOf(",") = 0 Then
s = s.Remove(0, 1)
End If
If s.LastIndexOf(",") = (s.Length - 1) And s.Length > 0 Then
s = s.Remove(s.Length - 1, 1)
End If
s = s.Replace(",,", ",")
Else
'If the column has been sorted in ascending order, we change it to descending order
s = s.Replace(e.SortExpression.ToString(), e.SortExpression.ToString() & " DESC")
End If
Else
'If the column has not been included
If s Is Nothing Then
s = e.SortExpression.ToString()
Else
If s.Length = 0 Then
s = e.SortExpression.ToString()
Else
'If another column already exists then include the new column retaining the earlier one
s = s & "," & e.SortExpression.ToString()
End If
End If
End If
GridView1.Attributes("SortExpression") = s
BindGrid()
End Sub

4. Include a procedure to change the header based on sorting information.


Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType.ToString = "Header" Then
Dim i As Integer
Dim s As String
Dim lb As LinkButton
Dim img As Image
Dim sSort As String
sSort = GridView1.Attributes("SortExpression")
i = 0
While i <>
lb = e.Row.Cells(i).Controls(0)
s = lb.Text
'Insert image in cell if sorted on this field
If InStr(sSort, s) > 0 Then
img = New Image
If InStr(sSort, s & " DESC") > 0 Then
img.ImageUrl = "images/desc.png"
Else
img.ImageUrl = "images/asc.png"
End If
e.Row.Cells(i).Controls.Add(img)
End If
i = i + 1
End While
End If
End Sub

4. Obviously, you will need to call BindGrid on page load.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsCallback Then
BindGrid()
End If
End Sub

5. When you run the form, depending on the data that is available in dt, you could get the following output.

No comments: