In the last article Examination of the FormView Control I demonstrated that how you can use the templates inside the FormView to display different controls. In this article I will embed the GridView control inside the FormView control and then export the GridView control to an excel file.

Introduction:

In the last article Examination of the FormView Control I demonstrated that how you can use the templates inside the FormView to display different controls. In this article 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. Once, you have bind the GridView you will see the data on the screen as shown in the screen shot below:

Exporting the GridView to Excel:

The code for the exportation task is 99% same as that I already discussed in my article Exporting GridView to Excel. The only change is the GetGridView method which returns the GridView populated with the data. Take a look at the code below:

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. Please also download the sample file which are attached at the end of the article.

I hope you liked the article, happy coding!