动态合并单元格并隐藏gridview中的行并将其导出为ex​​cel

时间:2013-08-10 07:21:20

标签: c# asp.net

我通过选中/取消选中复选框将griddata导出为ex​​cel时隐藏列。我使用的代码如下

protected void btnpacklist_Click(object sender, EventArgs e)
{
    string printType = "";
    string Vrnumber = "";
    string userid = "";
    foreach (GridViewRow row in GridView2.Rows)
    {
        CheckBox chkBxHeader = GridView2.HeaderRow.FindControl("chkBxHeader") as CheckBox;
        CheckBox chkBxSelect = row.FindControl("chkBxSelect") as CheckBox;
        Label VrNo = row.FindControl("VrNo") as Label;
        if (chkBxHeader.Checked == true)
        {
            printType = "All";
            userid = ddlCustomer.SelectedValue;
        }
        else if (chkBxSelect.Checked == true)
        {
            printType = "selected";
            if (Vrnumber == "")
            {
                Vrnumber = "'" + VrNo.Text + "'";
            }
            else
            {
                Vrnumber = Vrnumber + "," + "'" + VrNo.Text + "'";
            }
        }
    }
    if (printType == "selected")
    {
        Response.Redirect("TransactionReport.aspx?Vrnumber=" + Vrnumber + "&printType=" + printType);
    }
    else if (printType == "All")
    {
        Response.Redirect("TransactionReport.aspx?userid=" + userid + " &printType=" + printType);
    }




    Response.Redirect("TestPrint.aspx");
}

我通过在网格的rowdatabound事件中使用以下代码来动态合并我的gridview的第一个单元格。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       DataRowView drv = ((DataRowView)e.Row.DataItem);
        if (previousname == drv["name"].ToString())
        {

            if (GridView1.Rows[firstRow].Cells[0].RowSpan == 0)
            {

                GridView1.Rows[firstRow].Cells[0].RowSpan = 2;
           }

            else
            {
                GridView1.Rows[firstRow].Cells[0].RowSpan += 1;
            }

           e.Row.Cells.RemoveAt(0);
        }
        else  
        {
            e.Row.VerticalAlign = VerticalAlign.Middle;


            previousname = drv["name"].ToString();

            firstRow = e.Row.RowIndex;

        }
    }

}

当我删除rowdatabound代码时,我在excel表中得到了正确的结果,但如果我使用行数据绑定代码,我会在excel表中添加额外的单元格和不正确的结果。请帮助我......我被困了......

提前致谢

0 个答案:

没有答案