Here is my code fro converting gridview to CSV. Gridview values are not autogenerated columns,its from database as templatefield.
try
{
DataTable dt1 = new DataTable();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=MyCsvFile.csv");
Response.ContentType = "application/text";
StringBuilder strBr = new StringBuilder();
strBr.Append("\n");
for (int j = 1; j < GridView1.Rows.Count; j++)
{
strBr.AppendLine("");
for (int k = 0; k < GridView1.HeaderRow.Cells.Count; k++)
{
strBr.Append(GridView1.Rows[j].Cells[k].Text.Replace(",", "") + ",");
}
strBr.Append("\n");
}
Response.Write(strBr.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
throw ex;
}
Here GridView1.Rows[j].Cells[k].Text , this "Text" contains null value. Please help...