GridView Export from inside FormView
By AzamSharp
Views: 13631

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!

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Export To excel
Name: Gauri
Date: 2/21/2007 8:06:37 AM
Comment:
Hi,

I liked your article especially the video.

I have my grid inside a conent placeholder.
Also my page has a master page.

I tried using all the things that you suggested to get rid of "Control 'ctl00_MainContentPlaceHolder_grdReport1' of type 'GridView' must be placed inside a form tag with runat=server" error.

But I still get it. Does it has anything to do with the content placeholder?

Can I directly export the dataset to excel, istead of using gridview.rendercontrol?

Thanks in advance.
Your help is greatly appreciated.

Gauri
Subject: Exporting DataSet to Excel
Name: AzamSharp
Date: 2/22/2007 6:45:07 PM
Comment:
Yes, you can export DataSet to excel. Simply fill the DataSet with the data and bind it to the GridView. Bind the GridView on the page and export the GridView using the normal code. It will work!
Subject: Export To excel
Name: Vineet
Date: 4/5/2007 6:13:02 AM
Comment:
Hi Gauri

I also had the same problem with the grid being inside the content placeholder and was giving the same error.

But as soon as i overrode the function

public override void VerifyRenderingInServerForm(Control control)


{


// Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.


}

It started functioining properly.

Thanks
Subject: Export To excel
Name: Rajnikant
Date: 5/10/2007 3:04:26 AM
Comment:
After enter this code - Error Gone !

Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

Thanks !
Subject: Issues with this implementation
Name: jon held
Date: 12/14/2007 8:59:53 AM
Comment:
I think there are a couple of issues with your implementation here:
1. Since you're using the same Cache object for every request, concurrent requests are going to clobber each other, and this implementation will break.
2. You don't consider the scenario where a GridView has multiple pages, so this implementation only exports the first page, when, ideally, what you want, is an export of all data from all pages.

Other than those two issues, it's pretty nifty code.
Subject: RE: Issues with this implementation
Name: AzamSharp
Date: 12/16/2007 4:12:19 PM
Comment:
Hi Jon Held, I am not sure what you mean by Cache object and clobber each other. I am simply putting the item inside the cache and retrieving it when I was the DataSet. For GridView with multiple pages I recommend that you check out the following article: http://www.koffeekoder.com/ArticleDetails.aspx?id=197
Subject: Gridview To Excell
Name: Pratik Solanki
Date: 2/3/2008 11:00:32 PM
Comment:
I realy appreciate your help.




Join WebHost4Life.com






Copyright GridViewGuy 2007-2008