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?

Recommended Answers

All 3 Replies

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?

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.