0

Hi I have a gridview which is bound to a datatable called datasource. The datatable is populated with data from the database which is then displayed in gridview. However I would like to add a title to the gridview, I tried this with gridiview.caption and it works fine. However users are able to download the gridview as a csv, the code for this is

 protected void downRep_Click(object sender, EventArgs e)
        {
            String name = Request.QueryString["name"];

            MemoryStream ms = new MemoryStream();
            core.Data.CSVDataWriter c = new CSVDataWriter();
            c.Export(ms, datasource);

            Response.Clear();
            Response.ContentType = "text/csv";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
            Response.BinaryWrite(ms.ToArray());
            Response.Flush();
            Response.Close();
            Response.End();
        }

as you can see it is exporting the data stored in datasource to the csv so it is unable to see the caption that is included in the gridview. Is there a way I can add the title to datasource?

2
Contributors
3
Replies
17
Views
2 Years
Discussion Span
Last Post by djjeavons
0

Hi

I haven't used the GridView Caption property before but I am assuming that this adds some form of title to the GridView which could span the number of columns? Is that correct?

If so, adding this data to a CSV file would break the structure as it is not really data. What are you trying to achieve as the end result? Is it that a user can use the data or that you want to prettify the data when read in something like Excel?

0

Yea that's exactly what the caption does.

All I really need is that when the user downloads the csv file it has a title at the top of the data, not the top of the file. So that when they print the csv they know what report they are looking at.

0

Well, one way would be to insert a row at the beginning of the table prior to writing it out to the CSV file that contains the GridView caption. I assume you can grab this property value.

        Dim newRow As DataRow = dataSource.NewRow
        newRow("Column1") = GridView.Caption
        dataSource.Rows.InsertAt(newRow, 0)

Then once the CSV file is created, delete the row so as not to affect your actual data.

        dataSource.Rows.RemoveAt(0)

HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.