Reorder columns programmatically

4 posts, 1 answers
  1. Marco
    Marco avatar
    23 posts
    Member since:
    Apr 2009

    Posted 24 Sep 2009 Link to this post

    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
  2. Answer
    Milan
    Admin
    Milan avatar
    1989 posts

    Posted 25 Sep 2009 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Marco
    Marco avatar
    23 posts
    Member since:
    Apr 2009

    Posted 25 Sep 2009 Link to this post

    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
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 26 Sep 2009 Link to this post

    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.
Back to Top