By Huseyin Altindag
Introduction
This simple application will demonstrate
-how to navigate with buttons in the data(Next, Previous, First, Last) and
-how to get connection to the database Nortwind(Customers, Orders).
A brief description
You can control navigation:
1.using a navigational control such as a ListBox or DataGrid.
2. you can also control navigation programmatically by directly interacting with the CurrencyManager.
There are 2 classes for the navigation in the data source
1.CurrencyManager
2.and BindingContext
that store a type of positional information that points to a single row.
As soon as you bind a data object to a control (i.e TextBox), .NET assigns automatically a CurrencyManager object which keeps track of the position in the data source.
Every Form has a Binding-Context that keeps track of all the CurrencyManager objects on the Form.
CurrencyManager derives from BindingManagerBase that manages all binding objects.
For more information : MSDN
Getting Started
We need 4 buttons to navigate(First, Next, previous, Last), 3 TextBox for data binding(table Customers) and the Windows Forms DataGrid control that displays data in a series of rows and columns.
In order to run this application you need first of all the right database connection.
Prepare the connection string which would contain the relevant information about the data store. Be sure to substitute your workstation id, data source(Sql Server name)
And initial catalog=Northwind.
SqlConnection con =new SqlConnection("workstation id=SONY;packet size=4096;integrated security=SSPI;data source=\"SONY\M" +
"YSQLSERVER\";persist security info=True;initial catalog=Northwind");
When you start the program you get database connection(Northwind(Customers, Orders) with CurrencyManagerForm_Load methode and the data from the table will be populated in the datagrid.
When you click First button,Position is set to 0(zero) because the first row starts by zero.
-Disable First and Previous buttons because there is no previous record in the data source and first row is being highlighted.
-Enable Next and Last Button because there are records forwards.
When you click Next button, Position in the data is increased by 1 and moved
to the next row
-As long as there are forwards records
Enable First,Previous buttons
-Otherwise disable Next, Last buttons wihich means you reached the limits
of the data source (End of records..).
When you click Previous button, Position in the data is decreased by -1 and moved
To the previous row.
-As long as there are records backwards,
enable Last and Next buttons
-Otherwise you reached beginning of therecords,
so disable First and Previous buttons
When you click Last button, Position in the data is set to the last record(row).
-Disable Next and Last buttons because there are no records forwards any more.
-Enable First and Previous buttons so that you can navigate backwards
To enable and disable the buttons I use the function(fn)/method
fnEnableDisableButtons with 4 arguments(2 buttons, string for StatusBar, Bool for true/false enabling/disabling)
For example:
//disable pushbuttons First, Previous
fnEnableDisableButtons(this.btFirst, this.btPrevious,"First record...",false);
//enable pushbuttons Last,Next
fnEnableDisableButtons(this.btNext, this.btLast,"",true);
private void fnEnableDisableButtons(Button bt1, Button bt2, string str, bool b)
{
bt1.Enabled=b;
bt2.Enabled=b;
this.statusBar1.Text=str;
}
WinDBTest.zip
Conclusion
This is just a short introduction to the database programming. There are certainly much more ways to probe and find out. I wanted to make a small contribution to it.
Thanks
Huseyin Altindag
haltindag@btopenworld.com