Microsoft KB Archive/322932

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

Article ID: 322932

Article Last Modified on 9/4/2003

-

APPLIES TO


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

-



This article was previously published under Q322932



For a Microsoft Visual Basic .NET version of this article, see 320566.

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 enables 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 Enable Editing in the Details Table
 Follow these steps to create a Visual C# .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 C# 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 declare the SqlClient namespace: using System.Data.SqlClient; </li>  Add the following code to the Form_Load event: DataSet ds = new DataSet; string stConnection = &quot;server=(local);integrated security=sspi;database=northwind&quot;;

SqlConnection cnNorthwind = new SqlConnection(stConnection); SqlCommand cmCustomers = new SqlCommand(&quot;select * from customers&quot;,cnNorthwind); SqlDataAdapter daCustomers = new SqlDataAdapter(cmCustomers); daCustomers.MissingSchemaAction = MissingSchemaAction.AddWithKey; SqlCommand cmOrders = new SqlCommand(&quot;select * from Orders&quot;,cnNorthwind); SqlDataAdapter daOrders = new SqlDataAdapter(cmOrders); daOrders.MissingSchemaAction = MissingSchemaAction.AddWithKey;

SqlCommand cmOrderDetails = new SqlCommand(&quot;select * from [Order details]&quot;,cnNorthwind); SqlDataAdapter daOrderDetails = 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 the Orders DataTables. //**********    DataColumn dcCustomers_customerID = new  DataColumn; dcCustomers_customerID = ds.Tables[&quot;Customers&quot;].Columns[&quot;CustomerID&quot;]; DataColumn dcOrders_CustomerID = new DataColumn; dcOrders_CustomerID = ds.Tables[&quot;Orders&quot;].Columns[&quot;CustomerID&quot;]; DataRelation drCustomer_To_Orders = new DataRelation(&quot;CustomerToOrder&quot;, dcCustomers_customerID, dcOrders_CustomerID); ds.Relations.Add(drCustomer_To_Orders);

//Create the DataRelation between the Orders and the Order details DataTables, //**********    DataColumn dcOrder_OrderID = new DataColumn; dcOrder_OrderID = ds.Tables[&quot;Orders&quot;].Columns[&quot;OrderID&quot;];

DataColumn dcOrderDetails_OrderID = new DataColumn; dcOrderDetails_OrderID = ds.Tables[&quot;details&quot;].Columns[&quot;orderID&quot;]; DataRelation drOrder_TO_Details = new DataRelation(&quot;OrdersTodetails&quot;, dcOrder_OrderID, dcOrderDetails_OrderID); ds.Relations.Add(drOrder_TO_Details);

DataGrid1.DataSource = ds.Tables(&quot;customers&quot;).DefaultView

//This code allows the user to edit the information in the DataGrid. //***********************************************       ds.Tables[&quot;orders&quot;].DefaultView.AllowEdit = false; ds.Tables[&quot;orders&quot;].DefaultView.AllowDelete = false; ds.Tables[&quot;Details&quot;].DefaultView.AllowEdit = false; ds.Tables[&quot;details&quot;].DefaultView.AllowDelete = false; //***********************************************                   </li> Modify the connection string to use your SQL Server-based computer.</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;) {    dataGrid1.ReadOnly = true; } else {    dataGrid1.ReadOnly = false; }                   </li>  Comment out the following code: ds.Tables[&quot;orders&quot;].DefaultView.AllowEdit = false; ds.Tables[&quot;orders&quot;].DefaultView.AllowDelete = false; ds.Tables[&quot;Details&quot;].DefaultView.AllowEdit = false; ds.Tables[&quot;details&quot;].DefaultView.AllowDelete = false; </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">