在gridview中添加空行的问题

时间:2015-04-25 12:02:16

标签: c# asp.net excel gridview

我从db表中获取数据,将其绑定到gridview,然后将数据从gridview导出到excel。它工作正常。

但是当我在数据绑定事件中使用以下代码在gridview顶部添加一行时:

protected void gvReports_DataBound(object sender, EventArgs e)
{
    if (gvReports.Rows.Count > 0)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

        int count = gvReports.HeaderRow.Cells.Count;
        TableHeaderCell cell = new TableHeaderCell();
        cell.BorderWidth = 0;
        cell.Text = "Commodity " + ddlCommMFArrival.SelectedItem.Text;
        cell.ColumnSpan = count - 2;
        row.Controls.Add(cell);
        row.BackColor = ColorTranslator.FromHtml("#f5fafd");
        gvReports.HeaderRow.Parent.Controls.AddAt(0, row);
    }
}

将Gridview导出到Excel的代码:

protected void btnExport_Click(object sender, EventArgs e)
{
    for (int i = 0; i < gvReports.Rows.Count; i++)
    {
        for (int j = 0; j < gvReports.Rows[i].Cells.Count; j++)
        {
            if (gvReports.Rows[i].Cells[j].Text == "NA")
            {
                gvReports.Rows[i].Cells[j].HorizontalAlign = HorizontalAlign.Right;
            }
        }
    }
    gvReports.HeaderRow.Cells[1].Visible = false;
    gvReports.HeaderRow.Cells[2].Visible = false;
    string filename = string.Empty;
    int ddlselect = Convert.ToInt32(ddlCommMFArrival.SelectedValue);
    if (ddlselect == 1)
    {
        filename = "MarketFee" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + "";
    }
    else if (ddlselect == 2)
    {
        filename = "Commodity Arrival" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + "";
    }

    //Export gridview data into MS Excel
    blExportToExcel blExport = new blExportToExcel();
    //Export gridview data into MS Excel
    blExport.ExportGridViewToExcel(gvReports, this, filename);
}

    public void ExportGridViewToExcel(GridView grv, System.Web.UI.Page pg, string fileName)
    {
        grv.AllowPaging = false;

        pg.Response.Clear();
        pg.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls");
        pg.Response.Charset = "";
        pg.Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        grv.RenderControl(htmlWrite);
        pg.Response.Write(stringWrite.ToString());
        pg.Response.End();

        grv.AllowPaging = true;
    }

然后它在位置0和excel中添加一个空行(将数据从gridview导出到excel)并截断gridview中的最后一行。任何人都知道这是什么问题?

1 个答案:

答案 0 :(得分:0)

尝试将HeaderRow添加逻辑移出DataBound并进入RowCreated。将其添加到GridView声明:

OnRowCreated="gvReports_RowCreated"

然后这是RowCreated(注意DataControlRowState.Insert):

protected void gvReports_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

        int count = e.Row.Cells.Count;
        TableHeaderCell cell = new TableHeaderCell();
        cell.BorderWidth = 0;
        cell.Text = "blah";
        cell.ColumnSpan = count - 2;
        row.Controls.Add(cell);
        row.BackColor = ColorTranslator.FromHtml("#f5fafd");
        gvReports.Controls[0].Controls.AddAt(0, row);
    }
}