Articles in this section
Category / Section

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

2 mins read

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

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied