1

我在通过选中/取消选中复选框将griddata导出到excel时隐藏列。我使用的代码如下

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");
}

我通过在网格的行数据绑定事件中使用以下代码来动态合并我的网格视图的第一个单元格。

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;

        }
    }

}

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

提前致谢

4

0 回答 0