开发者

Problem sorting a list inside a databound object

开发者 https://www.devze.com 2023-01-13 12:41 出处:网络
I have a List<ColumnList> ColumnListLists which has a binding source (bsLists) attached to it. ColumnList has a List<Column> inside of it. I have a binding source attached to the current o

I have a List<ColumnList> ColumnListLists which has a binding source (bsLists) attached to it. ColumnList has a List<Column> inside of it. I have a binding source attached to the current of bsLists pointing at that inner list.

Confused yet? here is some code that may help.

public class ColumnList
{
     ...
     public string Name { get; set;}
     public List<Column> ListOfColumns { get; set;}
}

public class Column
{
    ...
    public string HeaderName { get; set; }
}

public class CustContractsSetup
{
    public CustContractsSetup()
    {
        InitializeComponent();
        bsLists = new BindingSource(Properties.Settings.Default.ColumnListLists, null);
        cmbListName.DataSource = bsLists;
        cmbListName.DisplayMember = "Name";
        bsColumns = new BindingSource(bsLists, "ListOfColumns");
        lbCurrent.DataSource = bsColumns;
        lbCurrent.DisplayMember = "HeaderName";
    }
    BindingSource bsLists;
    BindingSource bsColumns;
    ListBox lbCurrent;
}

Now what I want to do is change the order of two Column's.

private void btnUp_Click(object sender, EventArgs e)
{
    if (lbCurrent.SelectedIndex <= 0 || lbCurrent.SelectedIndex > bsColumns.Count)
        return;
    System.Diagnostics.Debug.Print("before:");
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex - 2].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex - 1].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex + 1].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex + 2].HeaderName);
    ((ColumnList)bsLists.Current).ListOfColumns.Reverse(lbCurrent.SelectedIndex - 1, 1);
    Debug.Print("after:");
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex - 2].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex - 1].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex + 1].HeaderName);
    Debug.Print(((ColumnList)bsLists.Current).ListOfColumns[lbCurrent.SelectedIndex + 2].HeaderName);
    bsLists.ResetCurrentItem();
}

And this is the output I get

before:
Conversion Level
Conversion Programmer
Edge Required
Education Required
Target Month
after:
Conversion Level
Conversion Programmer
Edge Required
Education Required
Target Month

If things worked as they should Edge Required should have swapped with Conversion Progr开发者_开发技巧ammer. but as you can see the before and after list are exactly the same.

What kind of mistake am I making that is keeping my list from having the order changed?


I think your basic problem is the size of the Reverse range, try:

 .Reverse(lbCurrent.SelectedIndex - 1, 2);
0

精彩评论

暂无评论...
验证码 换一张
取 消