|
GridView Export from inside FormView |
|
Abstract:
|
|
In this tutorial I will embed the GridView control inside the FormView control and then export the GridView control to an excel file.
|
|
Introduction: |
In this tutorial I will embed the GridView control inside the FormView control and then export the GridView control to an excel file. |
|
The Scenario:
|
|
The scenario is that we have a FormView control and in its ItemTemplate there is a GridView control. Now, we want to populate the GridView control with some data and later export the GridView control to excel. This article is different from Exporting GridView to Excel in a sense that now the GridView is present inside the FormView control. |
Populating the GridView inside the FormView Control: |
|
The first thing that we need to do is to populate the GridView control with some dummy data so that we can export the data to the excel file. Take a look at the BindData method which populates the GridView with the data.
|
|
|
|
private void BindData()
{
DataSet ds = new DataSet();
if (Cache["DataSet"] == null)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad=new SqlDataAdapter("SELECT * FROM Users", myConnection);
ad.Fill(ds);
Cache["DataSet"] = ds;
}
fv1.DataSource = (DataSet)Cache["DataSet"];
fv1.DataBind();
FormViewRow row = fv1.Row;
GridView gv = row.FindControl("gv1") as GridView;
gv.DataSource = (DataSet) Cache["DataSet"];
gv.DataBind();
}
|
In the code above I have moved the DataSet inside the Cache object so that you don't have to make a trip to the database on every request. Later I have assigned the FormView control with the DataSet and bind it on the screen. The FormViewRow row = fv1.Row; get's the row from the FormView control which contains the GridView control. The FindControl method is used to locate the GridView control. Once, the GridView is found I simply assigns the DataSet to the GridView DataSource property and bind the GridView on the page.
|
|
Exporting the GridView to Excel:
|
|
|
|
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content- disposition","attachment;filename=FileName.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
//comment out the line below
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gv1 = GetGridView();
gv1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
}
|
And finally here is the GetGridView method: |
private GridView GetGridView()
{
fv1.DataSource = Cache["DataSet"] as DataSet;
fv1.DataBind();
FormViewRow row = fv1.Row;
GridView gv = row.FindControl("gv1") as GridView;
gv.DataSource = Cache["DataSet"] as DataSet;
gv.DataBind();
return gv;
}
|
The purpose of the GetGridView method is to return the populated GridView.Once, the populated GridView is returned it is exported to the Excel file. |
|
|