This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Again we create a new form, this time named 'blueFrm' and we give it a title.
We set the background color of the new form to blue in the third line of code.
Next we set the WindowsState of the Window to Maximized.
We assign the window to the parent window (MDI) and display it.
After you compile the code create a new Standard form and a Blue form.
As you saw, the Blue form is maximized. Click the 'Windows' menu and you will see the list of available openedforms and a checkmark next to the one selected. Using this menu you can easily switch between forms.
Finally, let's make the code for redFrm by double clicking the 'Red' item from the File -> New menu and inserting
In the first line we create a new form named 'standardFrm'. The new form is created like any other form. In the
second we give it a generic title.
In the third we specify the parent of the newly created window by setting MdiParent to the current main window(this). This makes the form appear in the MDI form.
Finally the form is displayed.
It's time to make the blueFrm. Double click the 'Blue' item from the File -> New menu.
After Visual C# .NET takes you to the part where you code, use the following code:
The first thing we'll need to do is generate a connection string to connect to our Access database. For simplicity,
the database I'm using doesn't require any authentication. If your database has authentication, MSDN has
some great documentation on how to accomplish that.string connString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\myDatabase.mdb";
The connection string is broken up into two parts, a provider and a data source. The provider is the engine we're
going to be using - in this case, Microsoft's Jet engine. The data source, for Access, is simply the path to thedatabase file.
Now let's use the connection string and get some data from our database.
//create the connection string string connString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\\myDatabase.mdb";
//create the database query string query = "SELECT * FROM MyTable";
//create an OleDbDataAdapter to execute the query OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results DataTable dTable = new DataTable();
//fill the DataTable dAdapter.Fill(dTable);
To keep the code simple, I've left out a lot of error handling. You'll definitely want to
surround dAdapter.Fill with some exception handling. That call will fail for many different reasons - for
instance the database isn't where the connection string says it is, or the query string is invalid SQL code.
So let's go through what this code is actually doing. The first thing to do is to create the connection string as
described above. Then we need an SQL statement to execute on our database. This can be any SELECT
statement you want. Next we create anOleDbDataAdapter which serves as a bridge between our DataTable and our database. An
OleDbCommandBuilder comes next. This beautiful object automatically generates SQL insert, update, and
delete statements to rectify changes made to our DataTable. Next we need to make a DataTable to hold the
information retrieved from the database. And lastly, we call dAdapter.Fill(dTable) which executes our SQL
query and fillsdTable with the results.
Now that we have a DataTable filled with database information, let's see how to synchronize it with
aDataGridView.//the DataGridView DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView BindingSource bSource = new BindingSource();
//set the BindingSource DataSource bSource.DataSource = dTable;
//set the DataGridView DataSource dgView.DataSource = bSource;
The BindingSource object is what will be keeping our DataTable synchronized with the DataGridView. So
we set the DataSource of the BindingSource to dTable, then set the DataSource of the DataGridView to
theBindingSource. Now when your program runs, the DataGridView should be filled with the results of your
SQL query.
At point, any changes made by the user in the DataGridView will automically be made to the DataTable, dTable.Now we need a way to get the changes back into the database. All you have to do is call the Update function of
theOleDbDataAdapter with the DataTable as the argument to accomplish this.