How to change the order of columns in Ultragrid?The scenario is, I have a table bind in the datasource. Example:The DataTable in dataSource bind in the ultragrid consist of column 1, column2, column3, column4 & column 5 and my Problem is i want to add an extra columns in between column3 & column4 but the extra columns I want to add is not part of a dataTable in dataSource. When I view the table in Ultragrid the extra columns i added appeared after column5 and i want to insert the extra columns between column3 & column 4. please help. tnx
Hi,
You can use the "VisiblePosition " property of the Header object of the desired column and set it to the desired value.
i.e.
ultraGrid1.DisplayLayout.Bands[0].Columns[0].Header.VisiblePosition = 1;
Hope it will help you.
I've been programmatically formatting some ultragrids using visibleposition and have been noticing some odd side effects, particularly with calculated object properties and button controls.
Does the Band insist the columns have a sequential VisiblePosition order under the hood? If I set a column to VisiblePosition 1000 will the Visible Position change to (maxcolumns-1)? If I change column 1's visible position to the same visible position of column 2, will column 2's visible position change to that of column 1? Is it safe to set visible positions starting with the left most column and working forwards?
Thanks
Jim
Spodgy said:Does the Band insist the columns have a sequential VisiblePosition order under the hood?
Yes.
Spodgy said:If I set a column to VisiblePosition 1000 will the Visible Position change to (maxcolumns-1)?
I'm not certain what actually happens here. What you've described is the most likely scenario; otherwise, I'd expect the grid to throw an exception.
Spodgy said:If I change column 1's visible position to the same visible position of column 2, will column 2's visible position change to that of column 1?
I believe so, but I'd have to test it to be certain, and I don't have an environment set up to do so right away. The way I recall this is by picturing "removing" the column from its current position, shifting all the columns between it and the destination to fill the space, then putting the column in the destination. However, I don't recall with 100% certainty what happens when you move a column toward the "right" (increasing its visible position), and it's not a suggested approach.
Spodgy said:Is it safe to set visible positions starting with the left most column and working forwards?
Not only is this safe, this is the suggested approach. By doing this, as you set subsequent column header positions, you won't "clobber" any positions you've already set.
Should it work if positions are not 0 1 2 3 4 5 6 and so on, but 0 4 5 6 2 3 1 ?
I have a problem that VisiblePosition doesn't work. May be here is the problem?
Thank you!
You need to set the positions in order from 0 up. Otherwise, a later position change might affect a previous one. For example, suppose you have 4 columns:
ABCD
And you want to arrange them like this:
DCAB
If you set the VisiblePositions in order of the columns like so:
columns["A"].Header.VisiblePosition = 2;columns["B"].Header.VisiblePosition = 3;columns["C"].Header.VisiblePosition = 1;columns["D"].Header.VisiblePosition = 0;
This will not work right. To understand why, let's go through the process step-by-step:
A = 2
BCAD
B = 3
CADB - We already run into a problem here, because column A has been shifted out of position by the movement of column B.
If we do in the position order, it works correctly:
D = 0
DABC
C = 1
A = 2 ( no change)
B = 3 ( no change)
What I have done in the past to make this easier for myself is create a class that stores the column and the position that I want without affecting the grid. Then sort the list in position order and apply them to the grid in order.
I have attached a sample project here with a class called WinGridColumnPositioner that does this. You just add the columns to the Positoner class itself and call the position method and it takes care of positioning the columns in order by position.
The WinGridColumnPositioner Class in VB for anyone still stuck in the ice age such as myself.
Private Class WinGridColumnPositioner
Private band As UltraGridBand Private columnPositions As Dictionary(Of Integer, UltraGridColumn) = New Dictionary(Of Integer, UltraGridColumn)
Public Sub AddColumnPosition(column As UltraGridColumn, visiblePosition As Integer)
If Me.band Is Nothing Then Me.band = column.Band End If
If column.Band IsNot Me.band Then Throw New ArgumentException("All columns added to the positioner must be from the same band") End If
If Me.columnPositions.Values.Contains(column) Then Throw New ArgumentException("Cannot add the same column to the list twice") End If
Me.columnPositions.Add(visiblePosition, column) End Sub
Public Sub Position() Dim columnCount As Integer = Me.columnPositions.Count If columnCount = 0 Then Throw New InvalidOperationException("There are no columns to position") End If
Dim proccessed As Integer = 0 Dim visiblePosition As Integer = 0
While (proccessed < columnCount) Dim column As UltraGridColumn Dim success As Boolean = Me.columnPositions.TryGetValue(visiblePosition, column) If success Then column.Header.VisiblePosition = visiblePosition proccessed += 1 End If visiblePosition += 1 End While End Sub End Class
Private Sub ArrangeMyUltraGridColumns() Dim columns As ColumnsCollection = Me.myUltraGrid.DisplayLayout.Bands(0).Columns Dim winGridColumnPositioner As WinGridColumnPositioner = New WinGridColumnPositioner() winGridColumnPositioner.AddColumnPosition(columns("A"), 0) winGridColumnPositioner.AddColumnPosition(columns("B"), 1) winGridColumnPositioner.AddColumnPosition(columns("C"), 2) winGridColumnPositioner.Position() End Sub
Is there a reason why setting VisiblePosition columns in order would result in some columns being out of the order specified? http://es.infragistics.com/community/forums/p/31757/385525.aspx#385525