Finding and setting grid column order

Posted by: kshitizkumar on 16 July 2019, 6:32 pm EST

    • Post Options:
    • Link

    Posted 16 July 2019, 6:32 pm EST - Updated 3 October 2022, 4:02 pm EST

    Hi,

    I am working on a windows application. I am showing certain columns in the grid and the user have an option to re-order the columns or remove any column. For this I have given a dialog for the user to re-order or remove any column from the list available. We had same application designed in vb6 with activeX version of the trueDBGrid control. There we just had to set the ‘grid.columns.order’ property to change the column’s desired index.

    Now the problem is when designing the application in c# using winforms edition, I am able to get the index of the column using ‘grid.splits[0].displaycolumns.indexof’ property. But since the property is read only, I am not able to set the desired index. I have attached screenshots of both vb6 and .net version. Please help.

    Regards,

    Kshitiz

  • Posted 17 July 2019, 3:52 pm EST

    Hello,

    Sorry but there is no direct method or property to set the column order like we have in ActiveX TrueDbGrid.

    In WinForms in order to achieve this result we need to use the RemoveAt and Insert methods of C1DisplayColumn class in the right manner to change the order of column.

    I have created a demo project to remove particular column and move columns particular column to specified position, please go through this and let me know if you find this useful.

    Note : IndexOf is a method which has a return type int, which tells us the position of column in the display window. We can not explicitly set the index of any column.

    For more information go through the following links :

    https://help.grapecity.com/componentone/NetHelp/c1truedbgrid/webframe.html#C1.Win.C1TrueDBGrid.4~C1.Win.C1TrueDBGrid.C1DisplayColumnCollection~IndexOf(C1DisplayColumn).html

    https://help.grapecity.com/componentone/NetHelp/c1truedbgrid/webframe.html#C1.Win.C1TrueDBGrid.4~C1.Win.C1TrueDBGrid.C1DataColumnCollection~IndexOf.html

    https://help.grapecity.com/componentone/NetHelp/c1truedbgrid/webframe.html#changingthecolumnord.html

    If you need any other help, please let me know.

    Thanks and Regards,

    Prabhat Sharma.

    TrueDbOrderColumnDemo.zip

  • Posted 17 July 2019, 9:57 pm EST

    Hi Prabhat,

    I went through your solution. I see what to try to did and it works but only when you try to reorder/remove one column at a time. But suppose we are in a loop that run through count of columns then X.splits[0].displaycolumns[posofCol] will not give correct column name because as we loop through all the columns the position of columns in a split will not remain consistent, it will keep changing. Is there a way to keep original order saved in somewhere so as to get column name correct ?

    Thanks

    Kshitiz

  • Posted 18 July 2019, 10:14 pm EST

    Hello,

    Sorry for the inconvinecne.

    When you reorder the columns, their indexs for c1TrueDBGrid.DisplayColumns will be changed but it will remain constant in c1TrueDBGrid.Columns i.e. C1DataColumnCollection.Because C1DataColumn represents the columns from datasource and C1DisplayColumn represents the columns in a split.

    I have created a sample where I have taken a list of shuffled column names of TrueDbGrid. I am iterating this list and reordering the columns in terms of their position in the list. After shuffling, if I check the names of the columns at particular indices, it gives different values for DataColumns and DisplayColumns collections as desired.

    For eg: If I check for index 3.

        c1TrueDBGrid1.Splits[0].DisplayColumns[3] : [b]Caption of Display Column at 3rd index[/b]
        c1TrueDBGrid1.Columns[3].Caption :[b] Caption of column in the original order at 3rd index[/b]
    

    So, original order will always be saved in the C1DataColumnCollection.

    If you are still facing any issue let me know and change the sample accordingly to replicate the issue.

    Thanks and Regards,

    Prabhat Sharma.

    TrueDbOrderColumnDemo.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels