如何克隆UltraGridColumn(Infragistics)?

时间:2012-09-05 20:17:57

标签: c# infragistics

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

1 个答案:

答案 0 :(得分:1)

要重构 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);
}