Click here to Skip to main content
13,554,805 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


22 bookmarked
Posted 10 May 2010
Licenced CPOL

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

, 29 May 2010
Rate this:
Please Sign up or sign in to vote.
How to create a simple Windows Forms C++ Database Application


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++.


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.

private: System::Void dataGridView1_CellContentClick
    (System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)
	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;

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:

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

and the connectionString is:

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

The calculatePopulation() Method

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

void calculatePopulation()
   newPopulation = currentPopulation + (rateofNaturalIncrease*currentPopulation );

The displayResults() Method

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

void displayResults()
	txtPopDoubling->Text=Math::Abs(populationDoubling) +""   ;
	txtNewPop->Text= newPopulation +"" ;
	txtPopGrowth->Text= Math::Abs(rateofNaturalIncrease*currentPopulation) +"" ;
		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_  +"";
	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.


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


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


About the Author

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.

You may also be interested in...


Comments and Discussions

QuestionTried to compile VS express 2010 Pin
norL18-Nov-12 16:13
membernorL18-Nov-12 16:13 
GeneralMy vote of 1 Pin
imagiro31-May-10 20:26
memberimagiro31-May-10 20:26 
General…those who can't, teach Pin
jarvisa18-May-10 1:55
memberjarvisa18-May-10 1:55 
GeneralRe: …those who can't, teach Pin
KarstenK18-May-10 2:24
memberKarstenK18-May-10 2:24 
RantMy vote of 1 Pin
Panic2k310-May-10 15:48
memberPanic2k310-May-10 15:48 
GeneralRe: My vote of 1 Pin
Johann Gerell10-May-10 21:42
memberJohann Gerell10-May-10 21:42 
GeneralRe: My vote of 1 Pin
Alan Beasley10-May-10 23:11
memberAlan 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid