)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: ASP.NET Web Forms |
Control: Grid |
Published Date: January 25, 2018 |
Last Revised Date: February 11, 2020

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:

Exported File

 

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Roger Alberto
Apr 10, 2019

Thank you. This is very usefull.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon 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.Close Icon

Live Chat Icon For mobile
Live Chat Icon