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

ListView binding columns order

5 Answers 379 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Constantinos Petridis
Top achievements
Rank 1
Iron
Iron
Constantinos Petridis asked on 05 Sep 2016, 07:52 AM

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.

5 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Sep 2016, 07:31 AM
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.
0
Constantinos Petridis
Top achievements
Rank 1
Iron
Iron
answered on 06 Sep 2016, 08:08 AM

Thank you, missed that :)

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

0
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
answered on 17 Oct 2017, 06:55 PM
My question here, is how do you know what column index is?  Yea, I can move from 5 to 1, but how do I know what column is currently index as 5?  If my underline data changes structure, then I could be moving the wrong column.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Oct 2017, 01:45 PM
Hello, Mark,  

Thank you for writing.  

Note that you can iterate the RadListView.Columns collection and find the desired column. Then, you can extract its index by using the Columns.IndexOf passing the ListViewDetailColumn. Here is demonstrated a sample code snippet:
foreach (ListViewDetailColumn column in this.radListView1.Columns)
{
    if (column.Name=="IsActive")
    {
        int columnIndex = this.radListView1.Columns.IndexOf(column);
    }
}

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

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
answered on 18 Oct 2017, 04:01 PM
Thanks, I should have looked how I was doing it for GRIDS, it is very similar...
Tags
ListView
Asked by
Constantinos Petridis
Top achievements
Rank 1
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Constantinos Petridis
Top achievements
Rank 1
Iron
Iron
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Share this question
or