Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms

How to Create a Simple Windows Forms C++ Database Application

Rate me:
Please Sign up or sign in to vote.
3.64/5 (8 votes)
29 May 2010CPOL2 min read 83.3K   10.8K   23   7
How to create a simple Windows Forms C++ Database Application
Image 1

Introduction

Designing apps in Microsoft Visual C++ 2005 Express Edition is pretty straight forward. Point this, click that, presto! place it on form. But seriously, click on the desired component (control), located at the left toolbar of Visual C++ 2005 Express Edition, and draw it at an appropriate location on the form.

Components on the form align to rectangular grids, giving your apps a symmetric look. This simple app demonstrates the ease of creating database applications and interacting with events in Windows Forms syntaxes, all in C++.

DataGridView

Fast forwarding a bit, we've created a Windows Form, placed a DataGridView on the form, plus some buttons and textboxes. Also we have connected an .mdb database file to DataGridView while in design mode.

To have the button respond to mouse clicks, we have to put some code into it. So we double-click on it and we are presented with an event method. All we have to do is place some code in that control's event method.

Let's click on a DataGridView and place relevant code in its dataGridView1_CellContentClick( ) section, to have it handle some events.

The dataGridView1_CellContentClick Event

The dataGridView1_CellContentClick event is called upon when the left button is pressed down while above a dataGridView cell.

C++
private: System::Void dataGridView1_CellContentClick
    (System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)
{
	textBox1->Text=dataGridView1->Rows[dataGridView1->
		CurrentCell->RowIndex]->Cells[0]->Value->ToString();
	textBox2->Text=dataGridView1->Rows[dataGridView1->
		CurrentCell->RowIndex]->Cells[1]->Value->ToString();
	textBox3->Text=dataGridView1->Rows[dataGridView1->
		CurrentCell->RowIndex]->Cells[2]->Value->ToString();
	textBox4->Text=dataGridView1->Rows[dataGridView1->
		CurrentCell->RowIndex]->Cells[3]->Value->ToString();
	textBox5->Text=dataGridView1->Rows[dataGridView1->
		CurrentCell->RowIndex]->Cells[4]->Value->ToString();
	country_=textBox1->Text;
	currentPopulation=( System::Convert::ToDouble(textBox2->Text));
	growthRatePercent=( System::Convert::ToDouble(textBox3->Text))/100;
	birthRate=        ((System::Convert::ToDouble( textBox4->Text))/100)/1000;
	deathRate=        ((System::Convert::ToDouble( textBox5->Text))/100)/1000;
	displayResults();
} 

The frmMain_Load() Method

When the Windows Form is loading, the frmMain_Load() method is called upon, in it are instructions to fill dataGridView1's cell with data from the database itself. The SQL command is:

SQL
SELECT country, Population, growthrate, birthrate, deathrate FROM data 

and the connectionString is:

SQL
provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"dataDbl.mdb\"
C++
private: System::Void frmMain_Load(System::Object^  sender, System::EventArgs^  e) {
	// TODO: This line of code loads data into the 'dataDblDataSet.data' table. 
         // You can move, or remove it, as needed.
	this->dataTableAdapter->Fill(this->dataDblDataSet->data);
	displayResults();
}  

The calculatePopulation() Method

The calculatePopulation() method is called upon to calculate the new population from the current one.

C++
void calculatePopulation()
{
   rateofNaturalIncrease=birthRate-deathRate;
   newPopulation = currentPopulation + (rateofNaturalIncrease*currentPopulation );
   populationDoubling=Math::Log(2)/rateofNaturalIncrease;
} 

The displayResults() Method

The displayResults() method is called upon to update the textboxes.

C++
void displayResults()
{
	calculatePopulation();
	txtPopDoubling->Text=Math::Abs(populationDoubling) +""   ;
	txtNewPop->Text= newPopulation +"" ;
	txtPopGrowth->Text= Math::Abs(rateofNaturalIncrease*currentPopulation) +"" ;
	txtOldPopulation->Text= 
		currentPopulation-(rateofNaturalIncrease*currentPopulation) +"" ;

	if ((birthRate*currentPopulation)-(deathRate*currentPopulation)>0)
	txtDiff->Text= (birthRate*currentPopulation)-(deathRate*currentPopulation) +"" ;
	else txtDiff->Text= (deathRate*currentPopulation)-
			(birthRate*currentPopulation) +"" ;

	txtDeaths->Text= deathRate*currentPopulation  +"";
	txtBirths->Text= birthRate*currentPopulation  +"";
	txtRateofNaturalIncrease->Text= rateofNaturalIncrease +"" ;
	txtDeathRate->Text= deathRate  +" ( "+deathRate*1000  +" per 1000)";
	txtBirthRate->Text= birthRate  +" ( "+birthRate*1000  +" per 1000)";
	txtcurPopulation->Text= currentPopulation  +"";
	txtCountry->Text= country_  +"";
	txtGrowthRatePercent->Text=growthRatePercent+"";
	txtPopulation->Text= currentPopulation+ "";
} 

And that is how easy it is to create a database application and interacting data using Windows Forms syntaxes in C++.

Thanks for reading.

History

  • 2010-05-08 Updates made
  • 2006-11-21 Code complete

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sweden Sweden
About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions

 
QuestionTried to compile VS express 2010 Pin
norL18-Nov-12 16:13
norL18-Nov-12 16:13 
GeneralMy vote of 1 Pin
imagiro31-May-10 20:26
imagiro31-May-10 20:26 
General…those who can't, teach Pin
jarvisa18-May-10 1:55
jarvisa18-May-10 1:55 
GeneralRe: …those who can't, teach Pin
KarstenK18-May-10 2:24
mveKarstenK18-May-10 2:24 
RantMy vote of 1 PinPopular
Panic2k310-May-10 15:48
Panic2k310-May-10 15:48 
Isn't this C++/CLI (.net) ? If so then it isn't C++.

Stop polluting the C++ RSS feeds...
GeneralRe: My vote of 1 Pin
Johann Gerell10-May-10 21:42
Johann Gerell10-May-10 21:42 
GeneralRe: My vote of 1 Pin
Alan Beasley10-May-10 23:11
Alan Beasley10-May-10 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.