要重构InitializeLayout方法,我的意思是提取为此方法编写的所有代码(通常为显示或网格的其他一次性配置格式化列)并将所有内容放在可直接从代码调用的不同方法中。
然后,当您的用户按下按钮打印网格时,使用相同的数据源初始化 gridReport,调用相同的公共代码并对第二个网格上的列执行特定的隐藏。
此伪代码假设您已经声明了两个网格(带有初始数据的 grdMain 和用于打印的 grdReport)我还假设存在一个 ultraGridPrintDocument 来启动打印过程
private void gridMain_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
CommonInitializeLayout(gridMain, e);
}
private void CommonInitializeLayout(UltraWinGrid grd, InitializeLayoutEventArgs e)
{
UltraGridBand b = e.Layout.Bands[0];
// Now do the customization of the grid passed in, for example....
b.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
b.Override.AllowAddNew = AllowAddNew.No;
b.Override.NullText = "(Not available)";
b.Columns["CustName"].Header.Caption = "Customer Name";
....... etc ....
}
private void cmdMakeReport_Click(object sender, EventArgs e)
{
// This assignment will trigger the InitializeLayout event for the grdReport
grdReport.DataSource = grdMain.DataSource;
// Now the two grids have the same columns and the same data
// Start to hide the columns not desired in printing
grdReport.DisplayLayout.Bands[0].Columns["CustID"].ExcludeFromColumnChooser =
ExcludeFromColumnChooser.True
grdReport.DisplayLayout.Bands[0].Columns["CustID"].Hidden = true;
// .... other columns to hide.....
// Now print the grdReport
ultraGridPrintDocument.Grid = grdReport;
ultraGridPrintDocument.Print();
}
private void gridReport_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
CommonInitializeLayout(griReport, e);
}