views:

676

answers:

2

Hi all,

I have a need to export the contents in a ComponentArt Grid into a file, preferably excel in csv format.

I was wondering if anyone had any ideas how to best approach this task. At the moment we have the grid being populated with data, and using client templating some minor manipulation is being performed before it is displayed to the user.

An example of a template that is applied is:

<ComponentArt:ClientTemplate Id="PostTemplate">
   ## DataItem.GetMember("LastPostBy").Value ##<br />## DataItem.GetMember("LastPostDate").Value ##
</ComponentArt:ClientTemplate>

Where the column definitions are:

<ComponentArt:GridColumn Width="140" HeadingText="Last Post By " DataCellClientTemplateId="PostTemplate" />
<ComponentArt:GridColumn DataField="LastPostBy" Visible="false" />
<ComponentArt:GridColumn DataField="LastPostDate" Visible="false" />

So when the grid is exported I would like the file to contain what is in the grid at the moment of export including any templated changes that may be visible if possible.

Thank you in advance for your assistance.

A: 

What I'd do is that I would define the component art control (along with templates) in a user control .ascx file. You can then render this control to a string using this little code snippet:

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
{
    theGrid.RenderControl(textWriter);
}

string gridContent = sb.ToString();

Now, you have the contents of the grid in HTML format. From there, assuming component art has good well formed markup, you could consider parsing the html as XML, and pulling the cell values out to turn into your CSV.

Assuming you want to allow the user to save the CSV file via a browser, you can just implement a custom http handler that renders the control, creates the CSV, then send that to the browser as "text/csv"

Joel Martinez
Sadly this does not work. It was the first thing I tried, as that would be how I would export a regular grid control.
Denis Sadowski
Are you saying that the CA grid doesn't render correct HTML? then perhaps your only option is to duplicate the transformations in the grid template into another area that is more easily reproducible
Joel Martinez
+2  A: 

You cannot export the Web.UI Grid directly using RenderControl into a text writer. Instead, you should convert its data into another form (such as the ASP DataGrid) and export that instead.

The reason behind this is that the Web.UI Grid's structure is build dynamically on the client -- it's passed from server to client as a bunch of XML and array data, which is converted into the tables and divs that you see in the window.

As a result you'll want to perform the export on the Grid's datasource, and then analyze the client templates to replicate what they would change when built dynamically on the client.

The following is a short example of how to export the CA grid it into excel:

public void ExportDataSetToExcel() {
    object theColl = gridEmployee.DataSource; //just set this as your grid's datasource.
    GridView excelGrid = new GridView();
    excelGrid.DataSource = theColl;
    excelGrid.ID = "Export";
    excelGrid.DataBind();
    excelGrid.AutoGenerateColumns = true;

    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("content-disposition", "attachment;filename=RegainExport.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    excelGrid.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}

So before binding the datasource of the GridView you will want to perform the ClientTemplate replication.

Cube Pirate