We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to export the Grid when we bind the SQL DataSource in ASP.NET?

This knowledge Base explains that how we export the Grid when we bind the SQL DataSource in ASP.NET.

 

Solution:

In ASP.NET Grid using Export() server method we can export the Grid into excel, PDF and word documents.

In Export() server method we passed the dataSource as a parameter and in this method we need to pass the dataSource as IEnumerable or Datatable.

When we bind SqlDataSource we need to retrieve the data from the database using select method of SqlDataSource. In select method we have to retrieve data using SelectCommand SQL string.

The following code example demonstrates how to we export the Grid when we bind the SQL DataSource in ASP.NET.

 

ASP.NET:

1. Render the Grid Control.

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

 

    <ej:Grid ID="FlatGrid" runat="server" OnServerExcelExporting="FlatGrid_ServerExcelExporting" OnServerPdfExporting="FlatGrid_ServerPdfExporting" OnServerWordExporting="FlatGrid_ServerWordExporting" AllowPaging="True">   

  

          <ToolbarSettings ShowToolbar="True" ToolbarItems="excelExport,wordExport,pdfExport" />

        </ej:Grid>

    <asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"

            SelectCommand="SELECT * FROM [Territories]"></asp:SqlDataSource>

 

</asp:Content>

 

 

2. Retrieving the data from the database using select method and passed the dataSource in export() server method.

 

protected void FlatGrid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)

        {

            ExcelExport exp = new ExcelExport();

            DataView data = (DataView)SqlData.Select(new DataSourceSelectArguments());

            DataTable dt = data.Table;

            exp.Export(FlatGrid.Model, dt, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-lime");

        }

 

protected void FlatGrid_ServerPdfExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)

        {

            PdfExport exp = new PdfExport();

            DataView data = (DataView)SqlData.Select(new DataSourceSelectArguments());

            DataTable dt = data.Table;

            exp.Export(FlatGrid.Model, dt, "Export.pdf", true, true, "flat-lime");

        }

 

        protected void FlatGrid_ServerWordExporting(object sender, GridEventArgs e)

        {

            WordExport exp = new WordExport();

            DataView data = (DataView)SqlData.Select(new DataSourceSelectArguments());

            DataTable dt = data.Table;

            exp.Export(FlatGrid.Model, dt, "Export.docx", true, true, "flat-lime");

        }

 

Result:

 

Article ID: Published Date: Last Revised Date: Platform: Control:
8342 01/25/2018 01/25/2018 ASP.NET Web Forms Grid
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.