|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis application will show a different way of how to navigate through records in a data source with a
Code and How it worksIf the program is started, first the connection to the database and then the data binding for all the controls on the private void Form1_Load(object sender, System.EventArgs e)
{
fnGetDatabaseConnection();
fnGetDataBindings();
}
private void fnGetDatabaseConnection()
{
//connection to a data source
OleDbConnection con=new OleDbConnection(conString);
//Initializes a new instance of the OleDbDataAdapter class.
OleDbDataAdapter dadapter=new OleDbDataAdapter(selectString, con);
//Initializes a new instance of the Dataset storing
//data in a disconnected cache
dataset=new DataSet();
//refreshes rows in the DataSet
dadapter.Fill(dataset, "studentTable");
}
Here is a brief introduction to Data bindingData binding is the ability to bind some elements of a data source with some graphical elements of an application. The data in Windows Forms is bound by calling Windows Forms controls (i.e.
Simple data binding can be performed either at design time using Complex data binding is useful if you bind a control to a list of values from a data source. The For example:// Simple DataBinding
textBox1.DataBindings.Add("Text", dataset, "studentTable.studentID");
The control BindingContextEvery Windows Form has a If you bind several controls to the same data source, they share the same For example:
Now here is the method for data binding. private void fnGetDataBindings()
{
//display the current row number in the Label control
this.label1.Text = "Record : " +
((this.BindingContext[dataset, "studentTable"].Position + 1).ToString());
//Data binding for the all TextBoxes
this.textBox1.DataBindings.Add("Text", dataset,
"studentTable.studentID");
this.textBox2.DataBindings.Add("Text", dataset,
"studentTable.studentName");
this.textBox3.DataBindings.Add("Text", dataset,
"studentTable.studentSubject");
//set the upper limit of the range of the TrackBar control
//count how many records in the table
//get the current position from the BindingContext object
//and assign to TackBar.Maximum poperties
this.trackBar1.Maximum =
this.BindingContext[dataset,"studentTable"].Count - 1 ;
//don´t forget: first row number is 0. that´s why Count-1
// The Maximum property sets the value of the track bar
// when the slider is all the way to the right.
}
TrackBar(Slider)There is only one point left to mention, namely the event handler for scrolling the
private void trackBar1_Scroll(object sender, System.EventArgs e)
{
int recordNr;
//Pass the table "studentTable" to the BindingContext as the bound control
// and pass the current position of the slider on the track bar to "recordNr"
recordNr=this.BindingContext[dataset,"studentTable"].Position =
this.trackBar1.Value ;
//increment by 1 because we don´t want recordNr beginning by 0
++recordNr;
//display the position in Label
this.label1.Text = "Record : " + recordNr.ToString();
//check if any records in the table
if ((this.BindingContext[dataset, "studentTable"].Position ) <
(this.BindingContext[dataset, "studentTable"].Count-1))
{
this.BindingContext[dataset,"studentTable"].Position +=1; //go to next row
}
else
{
//inform the user about the end of the records
csShowMessageBox.fnShowMessageBoxWithParameters("You've " +
"reached the end of the records",
"LAST RECORD: "+recordNr.ToString(),
MessageBoxButtons.OK,
MessageBoxIcon.Information,0,0);
}
//check if we are at the first row
//in the table and if yes inform the user
if (this.BindingContext[dataset, "studentTable"].Position ==1)
{
csShowMessageBox.fnShowMessageBoxWithParameters("You've reached" +
" the beginning of the records",
"FIRST RECORD: "+recordNr.ToString(),
MessageBoxButtons.OK,
MessageBoxIcon.Information,0,0);
}
}
In ConclusionI designed this program to show a different way of how to navigate through records in a data table with a
|
|||||||||||||||||||||||||||||||||||||||||||||||||||