reportGrid = new DataGridView();
foreach (DataGridViewColumn col in grid.Columns)
{
DataGridViewColumn newCol = new DataGridViewColumn();
newCol = (DataGridViewColumn)col.Clone();
reportGrid.Columns.Add(newCol);
}
我试图模仿上面的一些现有代码,这些代码适用于 DatagridView 但适用于 UltraGrid 但不确定如何克隆该列,我还查看了适用于 UltraGridRows 的 CopyFrom。
foreach (UltraGridColumn col in grid.DisplayLayout.Bands[0].Columns)
{
UltraGridColumn newCol = new UltraGridColumn(); //Errror here as well
//newCol = (UltraGridColumn)col.Clone();
newCol.CopyFrom(col);
reportGrid.DisplayLayout.Bands[0].Columns.Add(newCol);
}
要重构 InitializeLayout 方法,我的意思是提取为此方法编写的所有代码(通常格式化列以供显示或网格的其他一次性配置)并将所有内容放在可直接调用的不同方法中来自您的代码。
然后,当您的用户按下按钮打印网格时,使用相同的数据源初始化 gridReport,调用相同的公共(public)代码并对第二个网格上的列执行特定的隐藏。
这个伪代码假设你已经声明了两个网格(带有初始数据的 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);
}
我是一名优秀的程序员,十分优秀!