Click here to Skip to main content
Click here to Skip to main content

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

By , 29 May 2010
 

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.

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:

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

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.

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)

About the Author

Clark Kent SuperCoder
Sweden Sweden
Member
About me:
I attended programming School and I have a degree in three 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've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic 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 about a dozen small simple applications and games.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionTried to compile VS express 2010membernorL18 Nov '12 - 16:13 
GeneralMy vote of 1memberimagiro31 May '10 - 20:26 
General…those who can't, teachmemberjarvisa18 May '10 - 1:55 
GeneralRe: …those who can't, teachmemberKarstenK18 May '10 - 2:24 
RantMy vote of 1memberPanic2k310 May '10 - 15:48 
GeneralRe: My vote of 1memberJohann Gerell10 May '10 - 21:42 
GeneralRe: My vote of 1memberAlan Beasley10 May '10 - 23:11 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 30 May 2010
Article Copyright 2010 by Clark Kent SuperCoder
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid