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

Tagged as

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

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

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)

Share

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.

Comments and Discussions

 
QuestionTried to compile VS express 2010 PinmembernorL18-Nov-12 16:13 
I get this error message
 
Unknown type'HPCalcDatabase.dataDblDataSet'
 
New to this please advise how this can be fixed?
GeneralMy vote of 1 Pinmemberimagiro31-May-10 20:26 
General…those who can't, teach Pinmemberjarvisa18-May-10 1:55 
GeneralRe: …those who can't, teach PinmemberKarstenK18-May-10 2:24 
RantMy vote of 1 PinmemberPanic2k310-May-10 15:48 
GeneralRe: My vote of 1 PinmemberJohann Gerell10-May-10 21:42 
GeneralRe: My vote of 1 PinmemberAlan Beasley10-May-10 23:11 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid