Navigating Through Datasets Using Bound Controls

Because simple-bound controls display data only from a single record at a time, it's common to add navigation controls like the buttons you see in the ch09_05 example, Figure 9.25, to enable users to move from record to record. When the users click the << button, they move to the first record; when they click the >> button, they move to the last record; the > button moves to the next record, and the < button moves back one record.

Note also that the user 's current position is indicated in a label (customized to look like a text box) between the navigation buttons in Figure 9.25. And as you can see in that figure, the complex-bound controls, such as the data grid and the list box, also automatically indicate the current record as you move through the data in the dataset they're bound to.

The controls you see in the ch09_05 example are bound to the current record in the form's binding context. In early versions of Visual Studio programming (pre .NET), the current record was maintained by the data store using data cursors (which are supported in most data providers), but because you now work with data in disconnected datasets, that's no longer possible, which is why binding contexts, which can maintain the current record locally, were introduced. In particular, you use a Windows form's BindingContext property (this property is part of the Control class, which the Form class is based on), which holds a collection of BindingManagerBase objects, to set the current record for all controls in the form.

The properties of the BindingManagerBase class that we'll be using are the Position propertywhich returns or sets the position of the current record in the binding contextand Count , which returns the number of records in the binding context. Let's start getting to some code. Our first job is to report the user's current location in the label you see between the navigation buttons.

Displaying the Current Record

If you take a look at Figure 9.25, you'll see that the label between the navigation buttons displays the current location in the binding context with the text "Record 5 of 23", indicating that the user is currently looking at record number 5 of 23 total records. To get both the current position and the total number of records, we'll use a BindingManagerBase object, which we get by specifying the data object we're working with ( dataSet11 in this case), and the table in that data object ( authors in this example) in the collection of BindingManagerBase objects returned by the form's BindingContext property. When we get our BindingManagerBase object, we can use its Position and Count properties to determine where we are in the binding context. Here's how to convert that data into a string and display it in the label:

 private void Form1_Load(object sender, System.EventArgs e) {   sqlDataAdapter1.Fill(dataSet11);  label3.Text = "Record " +   (((this.BindingContext[dataSet11, "authors"].Position + 1).ToString() +   " of ") + this.BindingContext[dataSet11, "authors"].Count.ToString());  } 

That's how you display the current position and the total number of records in the binding context for controls bound to the authors table in dataSet11 . So far, so good. Now what about moving to the next record?

Moving to the Next Record

When the user clicks the > button, the next record in the dataset (if there is a next record) becomes the current record. We can do that easily enoughall we have to do is to increment the Position property in the BindingManagerBase object and then display the new location in the label between the buttons:

 private void button4_Click(object sender, System.EventArgs e) {  this.BindingContext[dataSet11, "authors"].Position =   (this.BindingContext[dataSet11, "authors"].Position + 1);   label3.Text = "Record " +   (((this.BindingContext[dataSet11, "authors"].Position + 1).ToString() +   " of ") + this.BindingContext[dataSet11, "authors"].Count.ToString());  } 

You might think it necessary to check whether there's a next record before attempting to increment the Position property, but in fact the BindingManagerBase object takes care of that for you. If you try to move beyond the end of the data in the dataset, the Position property will not be incremented. Now when the user moves to the new record in the dataset, all the controls that are simple-bound to that dataset will display the new current record, and the complex-bound controls will highlight the new current record.

Moving to the Previous Record

It's just as easy to move to the previous record as it is to move to the next record. Here's the code we can use to move to the previous record in the binding context when the user clicks the < button:

 private void button3_Click(object sender, System.EventArgs e) {  this.BindingContext[dataSet11, "authors"].Position =   (this.BindingContext[dataSet11, "authors"].Position - 1);   label3.Text = "Record " +   (((this.BindingContext[dataSet11, "authors"].Position + 1).ToString() +   " of ") + this.BindingContext[dataSet11, "authors"].Count.ToString());  } 

Note that, just as when we incremented the Position property, if we attempt to decrement the Position property to a location before the beginning of the data in a dataset, the Position property will not be changed.

Moving to the First Record

Click the << button in the ch09_05 example moves the users back to the first record in the bound dataset. This one's easy to implementjust set the Position property to 0 and then display the new position in the dataset like this:

 private void button2_Click(object sender, System.EventArgs e) {  this.BindingContext[dataSet11, "authors"].Position = 0;   label3.Text = "Record " +   (((this.BindingContext[dataSet11, "authors"].Position + 1).ToString() +   " of ") + this.BindingContext[dataSet11, "authors"].Count.ToString());  } 

Moving to the Last Record

And you can also let the users move to the last record in the dataset when they click the >> button. In this case, you just set the Position property to Count - 1 , subtracting 1 because Position is 0-based, and then display the new location this way:

 private void button5_Click(object sender, System.EventArgs e) {  this.BindingContext[dataSet11, "authors"].Position =   this.BindingContext[dataSet11, "authors"].Count - 1;   label3.Text = "Record " +   (((this.BindingContext[dataSet11, "authors"].Position + 1).ToString() +   " of ") + this.BindingContext[dataSet11, "authors"].Count.ToString());  } 

To sum up, you can use the BindingContext property to access the BindingManagerBase object for the binding you're interested in from the collection of such objects returned by the property. You use the BindingManagerBase object's Position and Location properties to navigate through the records in a dataset, automatically updating any controls bound to that dataset to match.


In this chapter, we've written our own navigation code from scratch, but there is another visual tool that will write this kind of code for you, the Data Form Wizard. You use this wizard to create a new data form. To start this wizard, choose Project, Add New Item, and then select the Data Form Wizard icon in the Templates box and click OK. Using this wizard, you can create data forms that will bind controls to all the fields in a dataset, as well as display navigation buttons, Add, Update, and Cancel buttons.

Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: