Reputation: 1517
reportGrid = new DataGridView();
foreach (DataGridViewColumn col in grid.Columns)
{
DataGridViewColumn newCol = new DataGridViewColumn();
newCol = (DataGridViewColumn)col.Clone();
reportGrid.Columns.Add(newCol);
}
I'm trying to mimic some existing code above that works for a DatagridView but for a UltraGrid but not sure how to Clone the column, I looked at CopyFrom as well which works for UltraGridRows.
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);
}
Upvotes: 2
Views: 2337
Reputation: 216313
To refactor the InitializeLayout method I mean to extract all the code written for this method (usually formatting columns for display or other one time configuration of the grid) and put everything in a different method directly callable from your code.
Then, when your user press the button to print the grid, initialize the gridReport with the same datasource, call the same common code and perform the specific hiding for the columns on the second grid.
This pseudocode assume you have declared two grid (grdMain with the initial data and grdReport to use for printing) also I assume the presence of a ultraGridPrintDocument to start the printing process
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);
}
Upvotes: 1