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 gobords
i m vb.net developer i used the ultragrid and bind with some source after that at run time i change the order of column in ultra grid in this way
Me.UltraGrid1.DisplayLayout.Bands(0).Columns("name of coloumnor id is write here").Header.VisiblePosition = 4
4 is example of position you are showing you can change with your choice. i also attach the sample code.
Hope it will be helpful to you.if u need more help please visit the below link
http://ahmadkhalid44.blogspot.com/
Regards:
Ahmad Khalid
Software Engineer
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://ko.infragistics.com/community/forums/p/31757/385525.aspx#385525
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.