Introduction
This article shows how to download GridView data into Excel and XML format in a web application using Master Pages. This article starts by giving brief details on populating the grid that has paging and sorting functionalities and then describes the challenges of a master page while downloading data into Excel format. The download in XML format is very clean.
Populate the grid
Create a web application or web site that has implemented master pages. Create a content page and place a grid view like the following code snippet.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<div id="dvBtns"> <!—div for buttons -->
<asp:ImageButton ID="imgExcel" runat="server" ToolTip="Dwonload in Excel format" ImageUrl="~/images/excel-icon.gif"
OnClick="imgExcel_Click" Visible="false" />
<asp:ImageButton ID="imgXML" runat="server" ToolTip="Dwonload in XML format" ImageUrl="~/images/xml-icon.gif" Width="24px" Height="24"
OnClick="imgXML_Click" Visible="false" />
</div>
<div id="dvData"> <!—div for grid -->
<asp:GridView ID="gvEmpData" AutoGenerateColumns="false" ShowHeader="true" HeaderStyle-BackColor="#4b6c9e"
HeaderStyle-ForeColor="White" AlternatingRowStyle-BackColor="#DEDEDE" runat="server"
EnableSortingAndPagingCallbacks="true" EnableViewState="true" AllowPaging="true"
AllowSorting="true" PageSize="15" Height="100%" OnPageIndexChanging="gvEmpData_PageIndexChanging"
EmptyDataText="No data available." OnSorting="gvEmpData_Sorting" Width="100%">
<Columns>
<asp:BoundField HeaderText="Login id" DataField="UserName" SortExpression="UserName" />
<asp:BoundField HeaderText="Display Name" DataField="DisplayName" SortExpression="DisplayName" />
<asp:BoundField HeaderText="Country" DataField="Country" SortExpression="Country" />
<asp:BoundField HeaderText="Role" DataField="Role" SortExpression="Role" />
</Columns>
</asp:GridView>
</div>
</asp:Content>
In the code behind, add a method to populate the grid “gvEmpData” with employee details, such as in the following code snippet:
void loadEmpDataIntoGrid()
{
try
{
DataTable dtEmpData = GetEmpData();
if (dtEmpData != null && dtEmpData.Rows.Count > 0)
{
gvEmpData.DataSource = dtEmpData;
gvEmpData.DataBind();
}
else
{
gvEmpData.DataBind();
}
}
catch (Exception ex)
{
//Log error in exception handling module;
}
}
private DataTable GetEmpData()
{
DataTable dtEmpData = null;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL_ConnectionString"].ToString()))
{
SqlCommand command = new SqlCommand("GetEmpData", conn); //SP to get emp data
conn.Open();
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
ViewState["Employees"] = ds;
if (ds.Tables[0].Rows.Count > 0)
dtEmpData = ds.Tables[0];
conn.Close();
}
return dtEmpData;
}
Now we are done with populating the employee data into the grid. Next is to provide the implementation for paging and sorting.
Paging and Sorting
protected void gvEmpData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEmpData.PageIndex = e.NewPageIndex;
DataSet ds = ViewState["Employees"] as DataSet;
DataTable dt = ds.Tables[0];
if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
dt.DefaultView.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();
gvEmpData.DataSource = dt;
gvEmpData.DataBind();
}
#region Sorting
protected void gvEmpData_Sorting(object sender, GridViewSortEventArgs e)
{
//Retrieve the DataTable from the ViewState object.
DataSet ds = ViewState["Employees"] as DataSet;
DataTable dt = ds.Tables[0];
if (dt != null)
{
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression, "Employees");
gvEmpData.DataSource = dt;
gvEmpData.DataBind();
}
}
private string GetSortDirection(string column, string grid)
{
string sortDirection = "ASC";
string sortExpression = "";
string lastDirection = "";
if (grid == "Employees")
{
sortExpression = ViewState["SortExpression"] as string;
lastDirection = ViewState["SortDirection"] as string;
}
if (sortExpression != null)
{
//Otherwise, the default value can be returned.
if (sortExpression == column)
{
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
if (grid == "Employees")
{
//Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
}
return sortDirection;
}
#endregion
Anu VPosted Aug 17, 2016, 5:46 AM
Nice