C# step by step

begin programme
posts - 2, comments - 8, trackbacks - 0, articles - 0

Using the CurrencyManager

Posted on 2006-10-26 20:14 开始编程 阅读(1590) 评论(2)  编辑 收藏 引用
昨天写一个代码感觉CurrencyManager运用在DataGrvdView中用处很大 所以摘录一些内容来学习参考


You use the CurrencyManager object if you want to keep data-bound controls synchronized with each other which means showing data from the same record. For example: if you want to add a TextBox control to a form and bind it to a column of a table (e.g., Customers.FirstName) in a DataSet (e.g., dSet), the control is going to communicate with the BindingContext object for this form. In turn, the BindingContext object is going to talk to the specific CurrencyManager object for the data the TextBox control is binding.

Every Windows Form has a BindingContext object keeping track of all the CurrencyManager objects on the Windows Form. CurrencyManager keeps track of the position in the data source. When you bind a data object to a control (i.e., TextBox), a CurrencyManager object is automatically assigned. If you bind several controls to the same data source, they share the same CurrencyManager.

In a normal case where you are using an ADO.NET database (connecting and closing database) and displaying the records, e.g., in a DataGrid, you never need the CurrencyManager object. But if you want to know the exact position within a data structure (e.g., table in your database) as I did, you have to use the CurrencyManager object because the CurrencyManager has the Position property for this purpose. You can, for example, manipulate the Position property in a Next or Previous or First or Last button which I did in my program as well.
For example:
If you want to know how many records are in a DataTable, you simply query the BindingContext object's Count property
this.BindingContext[dataset1,"PersonTable"].Count - 1 ;
If you want to get the current position from the BindingContext object:
this.BindingContext[dataset1, "PersonTable"].Position + 1;

After data binding, you call and initialize CurrencyManager for your table. Here is the method I used to initialize the CurrencyManager for the table "PersonTable":
public void fnSetCurrencyManager()
{
  currManager = (CurrencyManager)this.
  BindingContext [ datc.dSet.Tables["PersonTable"]] ;
}

Feedback

# re: Using the CurrencyManager  回复  更多评论   

2006-11-01 14:45 by e商
不错。

# re:   回复  更多评论   

2006-11-01 16:12 by 天天影院
很详细,谢谢啊
只有注册用户登录后才能发表评论。