Showing posts with label Resources. Show all posts
Showing posts with label Resources. Show all posts

Friday, September 10, 2010

Optimized Paging and Sorting in ASP.NET GridView

nbsp;Introduction

Paging and sorting are most commonly used features of ASP.NET GridView. And it is very easy to use/implement these features in GridView with small chunk of lines. In this article I am going to demonstrate what are the performance drawbacks of using conventional way to page and sort your GridView and then I will demonstrate 'An Optimized way to implement Paging and Sorting'.
What are conventional steps for Paging and Sorting?

Usually we perform the following steps to enable paging and sorting in our GridView.

1. Set AllowPaging and AllowSorting Properties of GridView to True to enable paging and sorting respectively e.g
view sourceprint?
1.<asp:GridView ID="GridView1" runat="server" AllowPaging="true" AllowSorting="true"  >
2. </asp:GridView>

2. Set the PageSize property to mention how many records will be display on each page.

3. Set the SortExpression property of each column. By default each Data Bound columns has the bounded column name as default value for the SortExpression property.

4. Handle PageIndexChanging and Sorting Events of GridView to respond to paging and sorting actions respectively, like so:
view sourceprint?
<asp:GridView ID="GridView1" runat="server" AllowPaging="true"
            AllowSorting="true" onpageindexchanging="GridView1_PageIndexChanging"
            onsorting="GridView1_Sorting"  >
        </asp:GridView>
 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}

5. Put some logic in the event handlers to do their jobs


Download and Share Visual Studio Color Schemes

The good news is that Visual Studio allows you to completely customize the editor background and text colors to whatever you want - allowing you to tweak them to create the experience that is "just right" for your eyes and personality. You can then optionally export/import your color scheme preferences to an XML file via the Tools->Import and Export Settings menu command.




Manually sorting and paging Gridview without using datasource control

Many a times while working with Gridview we want to work with the paging and sorting functionality without using any datasource control. Gridview is flexible enough to perform these tasks without the use of any datasource control and only a few lines of code.  
For paging a Gridview manually we have to handle the Gridview’s PageIndexChanged Event.  In the event we need to change the PageIndex of Gridview to the page that user has clicked and again bind the Gridview.
protected void gvPaging_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gvPaging.PageIndex = e.NewPageIndex;
   gvPaging.DataBind();
}

This is all you need to do to make manual paging in the Gridview.
For manually sorting in Gridview we need to handle the Sorting event of Gridview. Here is the code to do it.
protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dtSortTable = gvSorting.DataSource as DataTable;
   if (dtSortTable != null)
   {
      DataView dvSortedView = new DataView(dtSortTable);
     dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
      gvSorting.DataSource = dvSortedView;
      gvSorting.DataBind();
   }
}

Export to PDF file in ASP.Net – Gridview to PDF, ASPX Page Content to PDF


Creating data driven application is one of the most commonly done task in our day today application development. In these applications, reporting and generating reports in various formats is one of the repeated requirements we will get. Read the article Export to Excel in ASP.Net 2.0 – Gridview to Excel, DataTable to Excel which discusses some handful of techniques to export the data to excel file. Apart from excel reports, generating PDF report is one of the most common reporting requirement we will get in data driven application. Exporting data to PDF format is not a straight forward task like we do for excel file generation. To do this, we have to employ some 3rd party component or develop a component ourselves by understanding the PDF file standards.

Since, developing a component ourselves is complicated and time consuming task we can try using any free 3rd party component available in the market. In this article, we will try using one of most widely used PDF generation component called iTextSharp or iText PDF component.

Get resource Here



http://www.codedigest.com/Articles/ASPNET/344_Export_to_PDF_file_in_ASPNet-Gridview_to_PDF_ASPX_Page_Content_to_PDF.aspx