0

Hi I am trying to export a gridview to CSV except i am getting an error due to some of the columns in the gridview not being visible. Was wondering if anyone knows how to fix this or a different way to export to csv.

 protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[8].Visible = false;
        }

        protected void export_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "test.csv"));
            Response.ContentType = "application/text";
            gv.AllowPaging = false;
            gv.DataBind();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < gv.Columns.Count; i++)
            {
                if (gv.Columns[i].Visible)
                {
                    sb.Append(gv.Columns[i].HeaderText + ',');
                }
            }

            sb.Append("\n");
            for (int j = 0; j < gv.Rows.Count; j++)
            {
                for (int k = 0; k < gv.Columns.Count; k++)
                {
                    sb.Append(gv.Rows[j].Cells[k].Text + ',');
                }
                sb.Append("\n");
            }
            Response.Write(sb.ToString());
            Response.End();

Edited by pritaeas: Moved to ASP.NET

2
Contributors
1
Reply
4
Views
2 Years
Discussion Span
Last Post by faheemsial
0

Plz share error string, meanwhile try

cell[0].attributes.add("display","none")

instead of setting it visible=False

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.