如何在PrintPreview窗口中隐藏dx:GridControl的一列

本文关键字:GridControl 一列 dx PrintPreview 窗口 隐藏 | 更新日期: 2023-09-27 18:02:28

我有一个DevExpress GridControl,其中包含许多列。现在在打印/导出期间,我想隐藏一列。逻辑是什么?我怎么能做到呢?

如何在PrintPreview窗口中隐藏dx:GridControl的一列

protected void PrintAllPages(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.HeaderRow.Cells[9].Visible = false;
        GridView1.FooterRow.Cells[9].Visible = false;
        // Loop through the rows and hide the cell in the first column
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            row.Cells[9].Visible = false;
        }
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        hw.AddStyleAttribute(HtmlTextWriterStyle.Direction, "rtl");
        GridView1.RenderControl(hw);
        string gridHTML = sw.ToString().Replace("'"", "'")
            .Replace(System.Environment.NewLine, "");
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type = 'text/javascript'>");
        sb.Append("window.onload = new function(){");
        sb.Append("var printWin = window.open('', '', 'left=0");
        sb.Append(",top=0,width=1000,height=600,status=0');");
        sb.Append("printWin.document.write('"");
        sb.Append(gridHTML);
        sb.Append("'");");
        sb.Append("printWin.document.close();");
        sb.Append("printWin.focus();");
        sb.Append("printWin.print();");
        sb.Append("printWin.close();};");
        sb.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
        GridView1.AllowPaging = true;
        GridView1.DataBind();
    }
    protected void PrintCurrentPage(object sender, EventArgs e)
    {
        GridView1.PagerSettings.Visible = false;
        GridView1.DataBind();
        GridView1.HeaderRow.Cells[9].Visible = false;
        GridView1.FooterRow.Cells[9].Visible = false;
        // Loop through the rows and hide the cell in the first column
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            row.Cells[9].Visible = false;
        }
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        hw.AddStyleAttribute(HtmlTextWriterStyle.Direction, "rtl");
        GridView1.RenderControl(hw);            
        string gridHTML = sw.ToString().Replace("'"", "'")
            .Replace(System.Environment.NewLine, "");
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type = 'text/javascript'>");            
        sb.Append("window.onload = new function(){");
        sb.Append("var printWin = window.open('', '', 'left=0");
        sb.Append(",top=0,width=1000,height=600,status=0');");
        sb.Append("printWin.document.write('"");
        sb.Append(gridHTML);
        sb.Append("'");");
        sb.Append("printWin.document.close();");
        sb.Append("printWin.focus();");
        sb.Append("printWin.print();");
        sb.Append("printWin.close();};");
        sb.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
        GridView1.PagerSettings.Visible = true;
        GridView1.DataBind();
    }

在点击事件中可以隐藏列。

每个GridView列都具有Visible and VisibleIndex属性。您可以简单地关闭Visible属性来隐藏一个列,然后打开它来再次显示它。如果您将此属性设置为-1,则列也将被隐藏。

void HideColumn(object sender, EventArgs e)
{
    colToHide.Visible=false;         
}

PrintPreview显示与源网格相同的列。要显示的列是在生成文档时截取的。在调用VisualDataNodeLinkCreateDocument method时,可以通过将Visible设置为false来隐藏所需的列。

更多信息请参考此链接https://www.devexpress.com/Support/Center/Question/Details/Q312869