ListView binding columns order

3 posts, 1 answers
  1. Constantinos Petridis
    Constantinos Petridis avatar
    21 posts
    Member since:
    May 2010

    Posted 05 Sep Link to this post

    I have a BindingList of a business object that I want to bind with a ListView (in detail mode).

    I can change which properties get displayed (and there respective column headers) using the ColumnCreating event, but I cannot change the order of the columns.

    As I understand, the binding process of ListView will use the properties in the order that they appear in code, this should have been fine, except that in case of object inheritance, the parent's class properties come after the child's class properties. In my case, I want base class properties to appear first and followed by child class properties.

    Is there a way to change or affect the order that columns get created?

    This would also allow me to save and recreate the order in which the columns appear, if, in future, I allow the user to reorder the columns of the ListView.

    I know that I could use a DataTable to manipulate the columns beforehand, and use this as my binding source, but I would like to avoid this "conversion" between business object and DataTable.

  2. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 06 Sep Link to this post

    Hello Constantinos,

    Thank you for writing.  

    You can reorder the columns programmatically after setting the DataSource property by using the RadListView.Columns.Move method. It is necessary to pass the old column index and the new one. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
     
        BindingList<B> items = new BindingList<B>();
     
        for (int i = 0; i < 5; i++)
        {
            items.Add(new B(i, "Item" + i, "Info" + i, i % 2 == 0));
        }
     
        this.radListView1.DataSource = items;
        this.radListView1.ViewType = Telerik.WinControls.UI.ListViewType.DetailsView;
     
        this.radListView1.Columns.Move(2, 0);
        this.radListView1.Columns.Move(3, 1);
    }
     
    public class A
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public A(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
     
    public class B : A
    {
        public string Info { get; set; }
     
        public bool IsActive { get; set; }
     
        public B(int id, string name, string info, bool isActive)
            : base(id, name)
        {
            this.Info = info;
            this.IsActive = isActive;
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Constantinos Petridis
    Constantinos Petridis avatar
    21 posts
    Member since:
    May 2010

    Posted 06 Sep in reply to Dess Link to this post

    Thank you, missed that :)

    Used the BindingCompleted event of listview to start the process of rearranging the columns.

Back to Top