[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Bind a DataGrid to a DataSet holding multiple tables at runtime in C#

Bind a DataGrid

This example shows how you can bind a DataGrid control to a DataSet loaded from an MDB file at runtime. When the form loads, the following code builds the DataSet and binds it to the form's DataGrid control.

// The DataAdapters and the DataSet. private OleDbDataAdapter DaAddresses, DaTestScores; private DataSet DsContacts; private void Form1_Load(object sender, EventArgs e) { const string SELECT_ADDRESSES = "SELECT * FROM Addresses"; const string SELECT_TEST_SCORES = "SELECT * FROM TestScores"; // Get the database file name. // This assumes the database is in the executable directory. string db_name = Application.StartupPath + "\\Contacts.mdb"; // Compose the connection string. string connect_string = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + db_name + ";" + "Persist Security Info=False"; // Create a DataAdapter to load the Addresses table. DaAddresses = new OleDbDataAdapter(SELECT_ADDRESSES, connect_string); // Create a DataAdapter to load the Addresses table. DaTestScores = new OleDbDataAdapter(SELECT_TEST_SCORES, connect_string); // Create and fill the DataSet. DsContacts = new DataSet("ContactsDataSet"); DaAddresses.Fill(DsContacts, "Addresses"); DaTestScores.Fill(DsContacts, "TestScores"); // Bind the DataGrid to the DataSet. dgContacts.DataSource = DsContacts; }

The code composes a database connect string and then creates two OleDbDataAdapter objects to select data from the database's Addresses and TestScores tables. It creates a new DataSet and uses the data adapters to load their tables into it. Finally the code sets the DataGrid control's DataSource property to the DataSet. The DataGrid automatically lets the user open either table and edit their values.

Note that this doesn't work with the DataGridView control. The DataGrid is more powerful. It not only can display a multi-table DataSet, but it also lets the user navigate between the tables if they are linked with foreign key relationships. (I'll demonstrate that in another post.)

When the user closes the form, the following code saves any changes in the data back to the database.

// Save changes to the data. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Use a CommandBuilder to make the INSERT, // UPDATE, and DELETE commands as needed. OleDbCommandBuilder command_builder; command_builder = new OleDbCommandBuilder(DaAddresses); command_builder = new OleDbCommandBuilder(DaTestScores); // Update the database. try { DaAddresses.Update(DsContacts, "Addresses"); DaTestScores.Update(DsContacts, "TestScores"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

This code creates two OleDbCommandBuilder objects to generate database commands as needed while updating the data. (When you create a command builder, it is attached to the data adapter you pass into the constructor. That's how the adapter can later generate the commands it needs.)

The code then calls each data adapter's Update method to update the database. (At that point the Update method uses the OleDbCommandBuilder objects to generate the commands it needs. It's all a bit mysterious.)

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.