14.7 Displaying a DataGrid

 <  Day Day Up  >  

14.7 Displaying a DataGrid

You want to use a DataGrid to display information from a database table.


Technique

The DataGrid , as its name implies, displays information from a database using an interface similar to a spreadsheet. Data is bound to the DataGrid using any of the methods discussed in this chapter. To create a DataGrid , create a new Windows Forms application and drag and drop the DataGrid control from the toolbox onto the Windows Form designer. All that is required to display data within the DataGrid is to set the DataSource property using a DataSet object, which can be either typed or untyped. Although you are certainly free to create the necessary code for the DataSet within your source file, the following method uses the Windows Form designer to generate the data access objects.

With the Windows Form designer open, open the Server Explorer tool window and locate the database table that you want to bind to the DataGrid . Once you find the table, simply drag and drop it onto the designer. This move automatically creates an SqlConnection and SqlDataAdapter object for you ( OleDbConnection and OleDbDataAdapter if you are using an Access database). Next, select the SqlDataAdapter from the component list within the designer, and click on the Generate Dataset Property Browser verb. After giving your DataSet object a name, select the DataGrid and set the DataSource property to the DataSet you just created.

The last step is to ensure the DataSet is filled when your application begins. Open the form's source code and locate the form constructor. Use the Fill method from the SqlDataAdapter object, passing the typed DataSet object that was created when you generated the DataSet . At this point, you can run the application and the DataGrid displays the values:

 
 public Form1() {     InitializeComponent();     sqlDataAdapter1.Fill( productsDS1 ); } 

While the user manipulates the data within the DataGrid , the underlying DataSet is also updated. However, because ADO.NET is working in a disconnected environment, those changes are not replicated back to the database. If you want to save any of the changes that are made to the DataSet , call the Update method defined in the SqlDataAdapter class, passing the DataSet object as a parameter. Listing 14.1 demonstrates how to detect whether any changes have been made to the DataSet object during the course of the session. If so, the user is prompted to save the database, and if the user chooses to save, the Update method is called. To determine whether a DataSet has been modified, check the HasChanges property:

Listing 14.1 Detecting DataSet Changes
 private void dgProduct_CurrentCellChanged(object sender, System.EventArgs e) {     if( productsDS1.HasChanges() )     {         statusBar1.Text = "Modified";     } } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) {     if( productsDS1.HasChanges() )     {         DialogResult result;         result = MessageBox.Show( this, "Would you like to save your changes?",             "Northwind Products", MessageBoxButtons.YesNoCancel,             MessageBoxIcon.Question );         if( result == DialogResult.Cancel )         {             e.Cancel = true;             return;         }         else if( result == DialogResult.No )         {             return;         }         else         {             sqlDataAdapter1.Update( productsDS1 );         }     } } 

Comments

The DataGrid control is a powerful control that allows you to create a database application in mere minutes. The features it supports would probably be enough to fill a book in itself. The technique in this section demonstrates the minimum essentials to create and use a DataGrid control within your application.

One thing you might have noticed when running an application with a DataGrid is the lack of any visual appeal . By default, the DataGrid simply renders a grid in white and black with a blue header. You can either change the several different properties that relate to the DataGrid 's style or choose one of several different canned formats by right-clicking on the DataGrid and selecting AutoFormat.

One interesting item to note is that a DataGrid doesn't have to be bound to a certain table within a DataSet . If you recall, a DataSet object can contain several tables of information, although this chapter has used the Products table for most examples. You can in fact set the DataSource property of the DataGrid to the DataSet itself rather than an individual table. When you do so, the DataGrid displays a tree control along the left side that, when expanded, contains links to each table within the DataSet . When you click a table link, the DataGrid expands and shows each cell within that table. Additionally, a back button within the DataGrid 's header allows you to go back to the list of tables.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net