Microsoft KB Archive/320566

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

Article ID: 320566

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



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
 * Requirements
 * Create the Sample to Enable Editing in the Details Table
 * Modify the Sample to Disable Editing in the Details Table
 * Troubleshooting

REFERENCES



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
 Follow these steps to create a Visual Basic .NET Windows application:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.  Drag a DataGrid control from the toolbox to the form.  Add the following code to the top of the form to import the SqlClient namespace: Imports System.Data.SqlClient </li>  Add the following code to the Form_Load event: Dim ds As New DataSet

Dim stConnect As String = &quot;server=(local);integrated security=sspi;database=northwind&quot; Dim cnNorthwind As New SqlConnection(stConnect)

Dim cmCustomers As New SqlCommand(&quot;select * from customers&quot;, cnNorthwind) Dim daCustomers As New SqlDataAdapter(cmCustomers) daCustomers.MissingSchemaAction = MissingSchemaAction.AddWithKey

Dim cmOrders As New SqlCommand(&quot;select * from orders&quot;, cnNorthwind) Dim daOrders As New SqlDataAdapter(cmOrders) daOrders.MissingSchemaAction = MissingSchemaAction.AddWithKey

Dim cmOrderDetails As New SqlCommand(&quot;select * from [order details]&quot;, cnNorthwind) Dim daOrderDetails As New SqlDataAdapter(cmOrderDetails) daOrderDetails.MissingSchemaAction = MissingSchemaAction.AddWithKey daCustomers.Fill(ds, &quot;Customers&quot;) daOrders.Fill(ds, &quot;Orders&quot;) daOrderDetails.Fill(ds, &quot;Details&quot;)

'Create the DataRelations between the Customers and Orders DataTables. '**********       Dim dcCustomers_customerID As New DataColumn dcCustomers_customerID = ds.Tables(&quot;Customers&quot;).Columns(&quot;CustomerID&quot;) Dim dcOrders_CustomerID As New DataColumn dcOrders_CustomerID = ds.Tables(&quot;orders&quot;).Columns(&quot;CustomerID&quot;)

Dim drCustomer_To_Orders As New DataRelation(&quot;CustomerToOrder&quot;, 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(&quot;Orders&quot;).Columns(&quot;OrderID&quot;) Dim dcOrderDetails_OrderID As New DataColumn dcOrderDetails_OrderID = ds.Tables(&quot;details&quot;).Columns(&quot;orderID&quot;) Dim drOrder_TO_Details As New DataRelation(&quot;OrdersTodetails&quot;, dcOrder_OrderID, dcOrderDetails_OrderID) 'Add the DataRelation to the Relations collection of the DataSet. ds.Relations.Add(drOrder_TO_Details)

DataGrid1.DataSource = ds.Tables(&quot;customers&quot;).DefaultView </li> Modify the connection string as appropriate for your environment.</li> Build and run the application.</li> 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.</li></ol>

back to the top

Modify the Sample to Disable Editing in the Details Table
  Stop running the application, double-click the DataGrid, and then add the following code in the DataGrid1_Navigate event: If DataGrid1.DataMember <> &quot;&quot; Then DataGrid1.ReadOnly = True Else DataGrid1.ReadOnly = False End If                   </li> Rebuild and run the application.</li> 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.</li></ol>

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

<div class="references_section">