2010-08-24 23 views
1

我有一个List<ColumnList> ColumnListLists,它有一个绑定源(bsLists)附加到它。 ColumnList在里面有一个List<Column>。我有一个绑定源绑定到当前的bsLists指向该内部列表。排序数据绑定对象中的列表的问题

困惑了吗?这里有一些可能有用的代码。

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; 
} 

现在我想要做的是改变两个Column的顺序。

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(); 
} 

这是输出我得到

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

如果事情的来龙去脉,他们应该边缘所需应转换程序员已经交换。但正如你所看到的,前后列表完全一样。

我犯了什么样的错误,是让我的列表不会改变顺序?

回答

1

我认为你的基本问题是倒车档的大小,请尝试:

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

我知道这是某种愚蠢的错误。 – 2010-08-24 20:27:17