以下是将gridview转换为CSV的代码。 Gridview值不是自动生成的列,它来自数据库作为模板字段。
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;
}
这里GridView1.Rows [j] .Cells [k] .Text,这个“Text”包含空值。 请帮忙......
答案 0 :(得分:0)
您需要在单元格内搜索控件,因为列为TemplateField
。
GridView1.Rows[j].Cells[k].FindControl('ControlId').Text