Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid sorting
 »  Home  »  Web Development  »  ASP.NET  »  Using ASP.NET DataGrid Web Server Control  »  DataGrid sorting
Using ASP.NET DataGrid Web Server Control
by Misha Zhutov | Published  11/03/2005 | Web Development ASP.NET | Rating:
DataGrid sorting

Sorting is quite an important built-in feature of DataGrid. It is really easy to implement a sorting functionality in DataGrid. To enable sorting in DataGrid you need to:

  • add AllowSorting="True" attribute for DataGrid
  • set SortExpression attribute from TemplateColumn element is equal to name of column in theDataTable.
  • add code in DataGrid.SortCommand Event handler to customize ascending and descending sorting:

Private Sub ProductsDataGrid_SortCommand(ByVal source As Object_
ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) _
Handles ProductsDataGrid.SortCommand
   If Not 
ProductsDataGrid.Attributes(SortExpressionAttributeIs Nothing Then

     Dim currectSortExpression As String = _
     ProductsDataGrid.Attributes(SortExpressionAttribute).ToString()

     If 
currectSortExpression.StartsWith(e.SortExpression.ToString()) Then

       If Not currectSortExpression.EndsWith(DescSortOrderThen
         ProductsDataGrid.Attributes(SortExpressionAttribute= 
         String.Concat(currectSortExpressionDescSortOrder)
       Else
         ProductsDataGrid.Attributes(SortExpressionAttribute= _
         currectSortExpression.Substring(0_
         currectSortExpression.Length - DescSortOrder.Length)
       End If
     Else
       ProductsDataGrid.Attributes(SortExpressionAttribute= _
       e.SortExpression.ToString()
     End If
  Else
    ProductsDataGrid.Attributes(SortExpressionAttribute= _
    e.SortExpression.ToString()
  End If

  SortDataBind()
End Sub

Generated using PrettyCode.Encoder

DataGrid Paging

When you display quite large amounts of data, it is not convenient to scroll it. DataGrid proposed a solution for this situation - built-in paging feature.
To enable paging in DataGrid you need to:

  • add AllowPaging="True" attribute for DataGrid
  • specify PagerStyle-Mode attribute for customizing paging mode.
  • Add PagerStyle element like this:
<PagerStyle NextPageText="Next" PrevPageText="Prev" HorizontalAlign="Center"         
CssClass="PagingText"></PagerStyle>
  • Set PageSize attribute which means number of items to display on a single page of the DataGrid control
  • Add code in the DataGrid.PageIndexChanged Event handler:

Private Sub ProductsDataGrid_PageIndexChanged(ByVal source As Object_
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
Handles ProductsDataGrid.PageIndexChanged
    ProductsDataGrid.EditItemIndex = -1
    ProductsDataGrid.CurrentPageIndex = e.NewPageIndex
    BindDataGrid()
End Sub
Generated using PrettyCode.Encoder
After loading a page, the user is presented a ‘Prev’ and ‘Next’ links at the bottom:
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 12/11/2005)
Rating
realy usefull,
 
Comment #2  (Posted by Johana on 12/28/2005)
Rating
El articulo es interante pero muy engorroso, mi pregunta es como podria, hacer que el segundo combo carge con una funcion que reciba 2 valores, el primero q sea resultado del codigo del primer combo y el segundo valor de un campo del grid, eso es lo q me gustaria hacer pero sin tantas clases no se si puedas resolver mi duda.
Gracias
 
Comment #3  (Posted by Misha Zhutov on 12/29/2005)
Rating
My apologies - I can't read Spanish
 
Comment #4  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #5  (Posted by an unknown user on 01/06/2006)
Rating
Very useful and demostrates very common operations that are used when programming with the datagrid.
 
Comment #6  (Posted by David Wilburn on 01/08/2006)
Rating
Very good article! For a person such as myself that has very little ASP.Net experience, it was very informative.

Much appreciated.
 
Comment #7  (Posted by an unknown user on 03/14/2006)
Rating
hi,
this is very good article.
 
Comment #8  (Posted by AFSUR HUSSAIN on 03/25/2006)
Rating
ya really its fine friends..,


 
Comment #9  (Posted by an unknown user on 04/18/2006)
Rating
good
 
Comment #10  (Posted by an unknown user on 05/19/2006)
Rating
I want to know data update in gtagrid.I can lern lot of thing in your article. Thanks lot for your help.
 
Comment #11  (Posted by an unknown user on 08/10/2006)
Rating
Great but:"The source code (both VB.NET and C#) is attached."...i cannot see it
 
Comment #12  (Posted by Misha Zhutov on 08/11/2006)
Rating
The attachment is on the last article's page, here is direct link on it: http://www.devcity.net/Download.aspx?FileType=Attachment&FileName=WebDataGridExamples.zip&File=3532cd2b-353d-4277-a05e-e6f20d4a268c.zip
 
Comment #13  (Posted by an unknown user on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #14  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #15  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #16  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Comment #17  (Posted by busrider on 01/04/2009)
Rating
eFront-???????? ??????? ??????? ? ???????????? ??????? www.e-front.com.ua
 
Sponsored Links