Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / Change the order of columns in Ultragrid

Change the order of columns in Ultragrid

New Discussion
pj
pj asked on Jun 5, 2013 11:35 AM

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

Sign In to post a reply

Replies

  • 0
    Sandeep Gupta
    Sandeep Gupta answered on Jul 4, 2008 1:42 PM

    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.

     

     

    • 0
      Jim
      Jim answered on Oct 24, 2008 4:59 PM

      Hi,

       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

       

      • 0
        [Infragistics] Vince McDonald
        [Infragistics] Vince McDonald answered on Oct 24, 2008 9:15 PM

        [quote user="Spodgy"]Does the Band insist the columns have a sequential VisiblePosition order under the hood?[/quote]

        Yes.

        [quote user="Spodgy"]If I set a column to VisiblePosition 1000 will the Visible Position change to (maxcolumns-1)?[/quote]

        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.

        [quote user="Spodgy"]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?[/quote]

        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.

        [quote user="Spodgy"]Is it safe to set visible positions starting with the left most column and working forwards?[/quote]

        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.

      • 0
        Yuri Artemenko
        Yuri Artemenko answered on Oct 10, 2012 9:10 AM

        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!

      • 0
        Yuri Artemenko
        Yuri Artemenko answered on Oct 10, 2012 9:11 AM

        I mean positions are not set in normal order.

      • 0
        Mike Saltzman
        Mike Saltzman answered on Oct 10, 2012 3:50 PM

        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:

        ABCD

        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:

        ABCD

        D = 0

        DABC

        C = 1

        DCAB

        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.

      • 0
        James Perry
        James Perry answered on Dec 13, 2012 3:39 PM

        Is there a reason why setting VisiblePosition columns in order would result in some columns being out of the order specified? http://www.infragistics.com/community/forums/p/31757/385525.aspx#385525

      • 0
        Erik Pratchenko
        Erik Pratchenko answered on Jun 4, 2013 2:13 AM

        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

  • 0
    Ahmad Khalid
    Ahmad Khalid answered on Jun 5, 2013 11:35 AM

    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

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
pj
Favorites
0
Replies
9
Created On
Jun 05, 2013
Last Post
13 years, 4 months ago

Suggested Discussions

Tags

Created by

pj

Created on

Jun 5, 2013 11:35 AM

Last activity on

Feb 12, 2026 8:52 AM