HOW TO: Enable and Disable DataTable Editing by Using the DataGrid in Visual Basic .NET (320566)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

This article was previously published under Q320566
For a Microsoft Visual C# .NET version of this article, see 322932.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Data.SqlClient

IN THIS TASK

SUMMARY

This step-by-step article demonstrates how to control editing tables in the DataGrid control on a table-by-table basis.

The Navigate event of the DataGrid only fires when you move from DataTable object to DataTable object, not record to record. When you move between DataTable objects, the DataMember property of the DataGrid is set to the relative relationship between DataTable objects. The sample in this article enable and then disables editing for the Details table.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET
  • Microsoft SQL Server 7.0 or Microsoft SQL Server 2000
back to the top

Create the Sample to Disable Editing in the Details Table

  1. Follow these steps to create a Visual Basic .NET Windows application:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
  2. Drag a DataGrid control from the toolbox to the form.
  3. Add the following code to the top of the form to import the SqlClient namespace:
    Imports System.Data.SqlClient
    					
  4. Add the following code to the Form_Load event:
    Dim ds As New DataSet()
    
            Dim stConnect As String = "server=(local);integrated security=sspi;database=northwind"
            Dim cnNorthwind As New SqlConnection(stConnect)
    
            Dim cmCustomers As New SqlCommand("select * from customers", cnNorthwind)
            Dim daCustomers As New SqlDataAdapter(cmCustomers)
            daCustomers.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
            Dim cmOrders As New SqlCommand("select * from orders", cnNorthwind)
            Dim daOrders As New SqlDataAdapter(cmOrders)
            daOrders.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
            Dim cmOrderDetails As New SqlCommand("select * from [order details]", cnNorthwind)
            Dim daOrderDetails As New SqlDataAdapter(cmOrderDetails)
            daOrderDetails.MissingSchemaAction = MissingSchemaAction.AddWithKey
     
            daCustomers.Fill(ds, "Customers")
            daOrders.Fill(ds, "Orders")
            daOrderDetails.Fill(ds, "Details")
    
    'Create the DataRelations between the Customers and Orders DataTables.
    '**********
            Dim dcCustomers_customerID As New DataColumn()
            dcCustomers_customerID = ds.Tables("Customers").Columns("CustomerID")
            Dim dcOrders_CustomerID As New DataColumn()
            dcOrders_CustomerID = ds.Tables("orders").Columns("CustomerID")
    
            Dim drCustomer_To_Orders As New DataRelation("CustomerToOrder", dcCustomers_customerID, dcOrders_CustomerID)
    
            'Add the DataRelation to the Relations collection of the DataSet.
            ds.Relations.Add(drCustomer_To_Orders)
    
    'Create the DataRelation between the Orders and Order details DataTables.
    '**********
            Dim dcOrder_OrderID As New DataColumn()
            dcOrder_OrderID = ds.Tables("Orders").Columns("OrderID")
            Dim dcOrderDetails_OrderID As New DataColumn()
            dcOrderDetails_OrderID = ds.Tables("details").Columns("orderID")
            Dim drOrder_TO_Details As New DataRelation("OrdersTodetails", dcOrder_OrderID, dcOrderDetails_OrderID)
           'Add the DataRelation to the Relations collection of the DataSet.
            ds.Relations.Add(drOrder_TO_Details)
    
            DataGrid1.DataSource = ds.Tables("customers").DefaultView
    					
  5. Modify the connection string as appropriate for your environment.
  6. Build and run the application.
  7. Try to edit any record in the Orders or the Details table. Notice that you can edit the table in the DataGrid even though you set the AllowEdit property of the DefaultView to False.
back to the top

Modify the Sample to Disable Editing in the Details Table

  1. Stop running the application, double-click the DataGrid, and then add the following code in the DataGrid1_Navigate event:
    If DataGrid1.DataMember <> "" Then
        DataGrid1.ReadOnly = True
    Else
        DataGrid1.ReadOnly = False
    End If
    					
  2. Rebuild and run the application.
  3. Try to edit any record in the Orders or the Details table. Notice that you have successfully disabled editing in the DataGrid by checking the DataMember property of the DataGrid in its Navigate event and by setting the ReadOnly property of the DataGrid to True.
back to the top

Troubleshooting

This sample uses the Northwind database in SQL Server. You can also use this procedure with Microsoft Desktop Engine (MSDE). However, the Northwind database is not included with MSDE. To use this procedure with MSDE, you must import the Northwind database from SQL Server or Microsoft Access. For more information about how to import the Northwind database, see SQL Server Books Online.

back to the top

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

310350 HOW TO: Update Parent-Child Data with an Identity Column from a Windows Forms Application by Using a Web Service

313485 INFO: Roadmap for ADO.NET DataSet, DataView, and DataViewManager Objects

back to the top

Modification Type:MajorLast Reviewed:9/4/2003
Keywords:kbHOWTOmaster kbSqlClient kbSystemData KB320566 kbAudDeveloper