Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 5 - Using Data Sources with the DataGridView C...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Using Data Sources with the DataGridView Control
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2006-11-09

    Table of Contents:
  • Using Data Sources with the DataGridView Control
  • How to generate a DataGridView control from a data source
  • How to edit the properties of a DataGridView control
  • How to edit the columns of a DataGridView control
  • A Product Maintenance application that uses a DataGridView control

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Data Sources with the DataGridView Control - A Product Maintenance application that uses a DataGridView control


    (Page 5 of 5 )

     

    At this point, the DataGridView control and binding navigator toolbar provide all the functionality needed for an application that can be used to maintain the data in the Products table. Figure 14-9 shows how this application appears to the user at runtime. It also presents the code that Visual Studio generates when you create this application, which includes everything that’s necessary to make it work. As a result, you can create an application like this one without having to write a single line of code. If you’ve ever had to manually write an application that provides similar functionality, you can appreciate how much work this saves you.

    When this application starts, the first event handler in this figure is executed. This event handler uses the Fill method of the TableAdapter object to load data into the DataSet object. In this example, the data in the Products table of the MMABooks database is loaded into the Products table of the dataset. Then, because the DataGridView control is bound to this table, the data is displayed in this control and the user can use it to modify the data in the table by adding, updating, or deleting rows.

    When the user changes the data in the DataGridView control, those changes are saved to the dataset. However, the changes aren’t saved to the database until the user clicks the Save button in the toolbar. Then, the second event handler in this figure is executed. This event handler starts by calling the Validate method of the form, which causes the Validating and Validated events of the control that’s losing focus to be fired. Although you probably won’t use the Validated event, you may use the Validating event to validate a row that’s being added or modified. You’ll see an example of that later in this chapter.

    Next, the EndEdit method of the BindingSource object applies any pending changes to the dataset. That’s necessary because when you add or update a row, the new or modified row isn’t saved until you move to another row.

    Finally, the Update method of the TableAdapter object saves the Products table in the DataSet object to the MMABooks database. When this method is called, it checks each row in the table to determine if it’s a new row, a modified row, or a row that should be deleted. Then, it causes the appropriate SQL Insert, Update, and Delete statements to be executed for these rows. As a result, the Update method works efficiently since it only updates the rows that need to be updated.

    Now that you understand this code, you should notice that it doesn’t provide for any exceptions that may occur during this processing. Because of that, you need to add the appropriate exception handling code for any production applications that you develop so that they won’t crash. You’ll learn how to do that next.

    The user interface for the Product Maintenance application

    The code that’s generated by Visual Studio

      Private Sub Form1_Load(ByVal sender As System.Object, _
             
    ByVal e As System.EventArgs) Handles MyBase.Load
         'TODO: This line of code loads data into the 'MMABooksDataSet.Products'
         'table. You can move, or remove it, as needed. 
         Me.ProductsTableAdapter.Fill(Me.MMABooksDataSet.Products)
      End Sub

      Private Sub ProductsBindingNavigatorSaveItem_Click( _
              ByVal sender As System.Object, ByVal e As System.EventArgs) _
              Handles ProductsBindingNavigatorSaveItem.Click
         Me.Validate()
         Me.ProductsBindingSource.EndEdit()
         Me.ProductsTableAdapter.Update(Me.MMABooksDataSet.Products)
      End Sub

    The syntax of the Fill method

      TableAdapter.Fill(DataSet.TableName) 

    The syntax of the Update method

      TableAdapter.Update(DataSet.TableName)

    Description

    • Visual Studio automatically generates the code shown above and places it in the source code file when you drag a data source onto a form. If necessary, you can edit this code.
    • The generated code uses the Fill and Update methods of the TableAdapter object that’s generated for the table to read data from and write data to the database. It also uses the EndEdit method of the BindingSource object to save any changes that have been made to the current row to the dataset.
    • The Validate method causes the Validating and Validated events of the control that is losing the focus to be fired. You can use the Validating event to perform any required data validation for the form.
    • Users of a DataGridView control can sort the rows by clicking on a column heading and can size columns by dragging the column separators to the left or right. They can also reorder the columns by dragging them if that option is enabled (see figure 14-7).

    Figure 14-9.   A Product Maintenance application that uses a DataGridView control


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Murach's Visual Basic 2005," published by...
     

    Buy this book now. This article is excerpted from chapter 14 of the book Murach's Visual Basic 2005, written by Anne Boehm (Murach, 2006; ISBN: 1890774383). Check it out today at your favorite bookstore. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway