CurrencyManager 类

           管理 Binding 对象的列表。

          CurrencyManager 从 BindingManagerBase 类派生。使用 BindingContext 返回一个 CurrencyManager 或一个 PropertyManager。返回的实际对象取决于传递给 BindingContext 的 Item 属性的数据源和数据成员。如果数据源为只能返回单个属性的对象(而不是对象列表),则该类型将为 PropertyManager。例如,如果指定一个 TextBox 作为数据源,则将返回一个 PropertyManager。另一方面,如果数据源为实现 IList、IListSource 或 IBindingList 的对象,则将返回一个 CurrencyManager

      Current 属性返回基础列表中的当前项。若要更改当前项,请将 Position 属性设置为新值。该值必须大于 0 而小于 Count 属性值。

如果基础数据源实现 IBindingList 接口,并且 AllowNew 属性设置为 true,则可以使用 AddNew 方法。

[C#]
private CurrencyManager myCurrencyManager;
 
 private void BindControl(DataTable myTable){
    // Bind a TextBox control to a DataTable column in a DataSet.
    textBox1.DataBindings.Add("Text", myTable, "CompanyName");
    // Specify the CurrencyManager for the DataTable.
    myCurrencyManager = (CurrencyManager)this.BindingContext[myTable];
    // Set the initial Position of the control.
    myCurrencyManager.Position = 0;
 }
 
 private void MoveNext(CurrencyManager myCurrencyManager){
    if (myCurrencyManager.Position == myCurrencyManager.Count - 1){
       MessageBox.Show("You're at end of the records");
    }
    else{
      myCurrencyManager.Position += 1;
    }
 }
 
 private void MoveFirst(CurrencyManager myCurrencyManager){
    myCurrencyManager.Position = 0;
 }
 
 private void MovePrevious(CurrencyManager myCurrencyManager ){
    if(myCurrencyManager.Position == 0) {
       MessageBox.Show("You're at the beginning of the records.");
    }  
    else{
       myCurrencyManager.Position -= 1;
    }
 }
 
 private void MoveLast(CurrencyManager myCurrencyManager){
    myCurrencyManager.Position = myCurrencyManager.Count - 1;
 }

原文地址:https://www.cnblogs.com/yitian/p/1383664.html