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();