

It may also be more efficient to save changes grouped by type: all inserts, all updates, all deletes. See if there are any changes before you go to the trouble of creating a new SqlDataAdapter and SqlCommandBuilder. For example, you can check the DataSet's HasChanges method to There are a few simple enhancements you can make.
DATAGRID VISUAL BASIC UPDATE
'Debug.WriteLine("*** INSERT ***") 'Debug.WriteLine(command_) 'Debug.WriteLine("*** UPDATE ***") 'Debug.WriteLine(command_) 'Debug.WriteLine("*** DELETE ***") 'Debug.WriteLine(command_) ' Save the changes.
DATAGRID VISUAL BASIC CODE
' Uncomment this code to see the INSERT, ' UPDATE, and DELETE commands. ' Make the CommandBuilder generate the ' insert, update, and delete commands. Private Sub Form1_Closing(ByVal sender As Object, ByVal e _Īs ) Handles _ Now use the SqlDataAdapter's Update method to update the database. The statements the SqlCommandBuilder creates. Uncomment the Debug.WriteLine statements in the following code to see It automatically creates those statementsįrom the SELECT statement. To create a SqlCommandBuilder attached to the data adapter. The INSERT, UPDATE, and DELETE statements it will use to update the data. Add a TableMapping if you did in the Load event handler.īecause you have only defined the data adapter's SQL SELECT statement, you still need to define In this routine, make a SqlDataAdapterĪs before. To save the changes, give the form a Closing event handler. When the user modifies the data, the changes are only saved to the DataSet, not to the database. ' Bind the DataGrid control to the Contacts DataTable.ĭgContacts.SetDataBinding(m_DataSet, "Contacts") Private Sub Form1_Load(ByVal sender As Object, ByVal e As _ĭata_adapter = New SqlDataAdapter(SELECT_STRING, _ĭata_("Table", "Contacts")

Private Const CONNECT_STRING As String = _ "SELECT * FROM Contacts ORDER BY LastName, FirstName" Private Const SELECT_STRING As String = _

You can use the DataGrid's properties if you want to restrict access. The user can add, edit, and delete records using the DataGrid with no more work from you. This makes the control automatically display all of the data Now bind the DataGrid control to the DataSet. Use the data adapter's Fill method to pull data from the Optionally add a TableMapping to map the (stupid) default table name Table returned by the connection toĪ more meaningful name (like Contacts for the Contacts table).Ĭreate a new DataSet object to hold the data. The details of this string will vary depending In the form's Load event handler, create a new SqlDataAdapter using the SQL SELECT statement that fetches theĭata and a connection string for connecting to the database. The program only uses three interesting types of objects: SqlDataAdapter, DataSet, and SqlCommandBuilder. The user still needs to push a button to loadĭata and there is no code at all to save changes.Īfter you dig into the objects for a while, however, you discover that this is actually a very simple problem. Perhaps even more amazing is the fact that the form doesn't do everything. If you use the VB.NET Data Form Wizard to display data in a grid, you get an amazingly complicated and confusing result. VB Helper: HowTo: Use a DataGrid in VB.NET
