我有以下情況。我有兩種用數據填充GridView的方法。這些如下:Gridview:導出爲csv問題
protected void btSearch_Click(object sender, EventArgs e)
{
lqPackWeights.WhereParameters.Clear();
ControlParameter cp = new ControlParameter();
cp.Type = TypeCode.String;
if (radBuyer.Checked)
{
cp.ControlID = "ddlProd";
cp.PropertyName = "SelectedValue";
lbRadMiss.Text = "";
}
else if (radProd.Checked)
{
cp.ControlID = "tbxProdAC";
cp.PropertyName = "Text";
lbRadMiss.Text = "";
}
else
{
cp.ControlID = "lbRadMiss";
cp.PropertyName = "Text";
lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";
}
cp.Name = "IDDesc";
lqPackWeights.WhereParameters.Add(cp);
GridView1.DataSourceID = "lqPackWeights";
GridView1.DataBind();
}
而且
protected void btnShowPaper_Click(object sender, EventArgs e)
{
ORWeightsDataClassesDataContext dbPa = new ORWeightsDataClassesDataContext();
int max = 0;
if (int.TryParse(txtbxHowMany.Text, out max))
{
var queryPa = dbPa.tblOnlineReportingCOMPLETEWeights
.Where(x => x.MaterialLevel == "Primary" && x.MaterialText == "Paper" && x.MemberId == "FM00012")
.OrderByDescending(x => x.ProductPercentage).Take(max);
GridView1.DataSource = queryPa;
GridView1.DataBind();
這些都工作得不錯的填充與他們approrpiate數據集GridView的。
我也有下面的代碼,出口gridview的內容,以CSV:
protected void btnExportCSV_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
到CSV的出口工作得很好在第一種情況下(即如果/否則,如果情況),但是當它涉及到第二種情況,只有標題被提取。
這是怎麼回事?我已經加入了幾個休息時間,它看起來像是正在執行csv代碼的輸出,但實際的GridView內容不會放在同一個csv文件中。
任何想法?
是否有可能在GridView是不實際尚未在你試圖導出時限(因而沒有行)? – 2009-11-25 09:46:11