This is a migrated thread and some comments may be shown as answers.

Reorder columns programmatically

3 Answers 352 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marco
Top achievements
Rank 1
Marco asked on 24 Sep 2009, 08:20 PM
Hi,
I'm using a DataTable as datasource of a RadGridView.   The columns are automatically created.
I want to set some characteristics to the columns based on information that I get from a db, like header, visibility and column order.  For the first two I'm using the AutoGeneratingColumn event and it's working perfectly.  I haven't found a way to reorder the columns programmatically.

I tried the ReorderColumns(oldIndex, newIndex), but I wasn't able to get the oldIndex using grid.Columns.IndexOf(e.Column).  It looks like grid.Columns doesn't have the complete list of generated columns.

Thanks in advance for your help!
Marco

3 Answers, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 25 Sep 2009, 07:07 AM
Hi Marco,

You could try to use the Loaded event or the DataLoaded event of RadGridView.

public partial class Window1 : Window  
{  
    public Window1()  
    {  
        InitializeComponent();  
 
        this.playersGrid.ItemsSource = Club.GetPlayers();  
        this.playersGrid.DataLoaded += new System.EventHandler<System.EventArgs>(playersGrid_DataLoaded);  
        this.playersGrid.Loaded += new RoutedEventHandler(playersGrid_Loaded);  
    }  
 
    void playersGrid_Loaded(object sender, RoutedEventArgs e)  
    {  
        this.playersGrid.ReorderColumns(0, 1);  
    }  
 
    bool gridConfigured = false;  
 
    // or  
    void playersGrid_DataLoaded(object sender, System.EventArgs e)  
    {  
        if (gridConfigured)  
            return;  
 
        this.Dispatcher.BeginInvoke(new Action(() => this.playersGrid.ReorderColumns(0, 1)));  
        gridConfigured = true;  
    }  

Currently it is essential to use the Dispatcher if using the DataLoaded event due to a bug in ReorderColumns. A fix for this bug will be available with this Friday's Latest Internal Build.

Sincerely yours,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Marco
Top achievements
Rank 1
answered on 25 Sep 2009, 09:29 PM
Milan,
Your suggestion worked perfectly, thank you.  Though, I noticed the method ReorderColumns is slow, and it becomes an issue with many columns.  Do you have a suggestion on how to speed up the ordering or is there any performance enhancement that you're planning on releasing anytime soon?

Thanks,
Marco
0
Vlad
Telerik team
answered on 26 Sep 2009, 06:06 AM
Hello Marco,

With Q3 release we will introduce columns virtualization which will improve the performance of this method.

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Paul
Top achievements
Rank 2
Iron
Iron
Iron
commented on 16 Mar 2023, 02:32 PM

This event fires before autogenerated columns are present.
Is there one that fires after the autogenerated columns have been added?
Martin Ivanov
Telerik team
commented on 21 Mar 2023, 11:45 AM

There is no event that fires after all autogenerated columns are added. This said, there are different techniques to get the columns after they are generated, but the specific approach will depend on the user scenario. For example, you can use the Loaded event, if the data is assigned initially. Or you can use the AutoGeneratingColumns event of RadGridView and check the expected columns count in case you know this information. Or maybe, you can use the DataLoaded event with a Dispatcher to wait a bit for the columns to get generated.
Paul
Top achievements
Rank 2
Iron
Iron
Iron
commented on 21 Mar 2023, 11:51 AM

Thank you, I think the DataLoaded event will work for me.
Tags
GridView
Asked by
Marco
Top achievements
Rank 1
Answers by
Milan
Telerik team
Marco
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or