Thursday, December 16, 2004

Navigating a .NET dataset

In Delphi 7 navigating a dataset was a snap. You could use a TdbNavigator, or simply call the methods Next or Prior.
In a Winforms application it is not obviuos how to do this. A way to do this is using a CurrencyManager.
A currencymangager manages a list of bindings. A binding binds a value of a property to an object, just as in binding data to a grid.
(Live used to be simple in Delphi 7 :-) )

To navigate a dataset the code looks something like this:

procedure frmMain.MoveRecord(Step : Integer);
cm : CurrencyManager;
cm := BindingContext[dsProjects, 'Project'] As CurrencyManager; cm.Position := cm.Position + Step;
Label1.Text := dsProjects.Tables[0].Rows[cm.Position].Item['pr_number'].ToString;

This codes moves the dataset Step records and shows the value of the field pr_number of the current row in a label.
Remark that when you sort your grid, you should also sort your dataset, otherwise they will not be synchronized.

No comments: