I have a grid which has 4 bands and the columns in band[3] are not arranged in order as in its parent bands. My primary requirement is to arrange the Band[3] columns.
This is a part of "Export to Excel" code. The grids are already displayed in the form.
I have tried the below approach -
private UltraGrid rearrangeCol(UltraGrid grid) { int bandCount = grid.DisplayLayout.Bands.Count; int band = 0; List<String> colPos = new List<string>(); for (int i = grid.DisplayLayout.Bands[band].Columns["EndurEndInv"].Index; i < grid.DisplayLayout.Bands[band].Columns.Count; i++) { colPos.Add(grid.DisplayLayout.Bands[band].Columns[i].Key); } band++; while (band < bandCount) { int initialColPos = grid.DisplayLayout.Bands[band].Columns["EndurEndInv"].Index; for (int i = initialColPos, j = 0; i < grid.DisplayLayout.Bands[band].Columns.Count && j < colPos.Count; i++, j++) { if(!grid.DisplayLayout.Bands[band].Columns[i].Key.Equals(colPos[j], StringComparison.InvariantCulture)) { grid.DisplayLayout.Bands[band].Override.AllowColSwapping = Infragistics.Win.UltraWinGrid.AllowColSwapping.WithinBand; int xcngPos = grid.DisplayLayout.Bands[band].Columns[colPos[j]].Index; grid.DisplayLayout.Bands[band].Columns.Insert(i, "Temp"); grid.DisplayLayout.Bands[band].Columns[xcngPos + 1].Swap(grid.DisplayLayout.Bands[band].Columns[i]); grid.DisplayLayout.Bands[band].Columns.Remove("Temp"); grid.DisplayLayout.Bands[band].Override.AllowColSwapping = Infragistics.Win.UltraWinGrid.AllowColSwapping.Default; } else continue; } band++; }; return grid; }
Actually, nothing happens when I use SWAP, the keys, index remains same.
Is there any better approach?
Hi,
I'm not sure I understand what you are trying to do. If the columns are in the order you want on the screen, then they should export to Excel in the same order. Are you saying that they are exporting in a different order than they are shown in the on-screen grid? If so, then something in your code must be changing the order when you are exporting.
The code you posted here is using the grid's DisplayLayout, so this will affect the on-screen grid. If you called this code at any point after the export process began, it will have no effect on the export, because the way exporting works is that the exporter creates a clone of the grid's DisplayLayout. So the cloned layout would not be affected by any changes you make to the grid's DisplayLayout after the clone is created.
If you want to modify the export without modifying the on-screen grid, then you should use the ExportStarted event of the UltraGridExcelExporter and use the Layout which is passed into the event via the event args. This is the cloned layout. .
Thanks for your reply Mike,
Let me first give you a context of what happens. There are say 5 columns A,B,.., E and 3 Bands. Now Col D is SUM(A,B) and Col E is SUM(B,C). While preparing the Grid, Col D & E are added at the last hence there position is at last but while displaying, the code changes the position of the columns and say the order is as - A,B,E,D,C. I want it in the same order in my Excel as well. There is a code already in place which uses Ultrawingrid.Rows & Rows.Cells to fetch the data.
Now if I use Displaylayout.rows throught the Export Methods then will I get the oredr as is displayed or the actual order of the Grid?