You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q322932
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


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

  1. Follow these steps to create a Visual C# .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 C# 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 declare the SqlClient namespace:
    using System.Data.SqlClient;					
  4. Add the following code to the Form_Load event:
         DataSet ds = new DataSet();     string stConnection = "server=(local);integrated security=sspi;database=northwind";     SqlConnection cnNorthwind = new SqlConnection(stConnection);					     SqlCommand cmCustomers = new SqlCommand("select * from customers",cnNorthwind);     SqlDataAdapter daCustomers = new SqlDataAdapter(cmCustomers);     daCustomers.MissingSchemaAction = MissingSchemaAction.AddWithKey;			     SqlCommand cmOrders = new SqlCommand("select * from Orders",cnNorthwind);     SqlDataAdapter daOrders = new SqlDataAdapter(cmOrders);     daOrders.MissingSchemaAction = MissingSchemaAction.AddWithKey;     SqlCommand cmOrderDetails = new SqlCommand("select * from [Order details]",cnNorthwind);     SqlDataAdapter daOrderDetails = 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 the Orders DataTables.     //**********     DataColumn dcCustomers_customerID = new  DataColumn();     dcCustomers_customerID = ds.Tables["Customers"].Columns["CustomerID"];     DataColumn dcOrders_CustomerID = new DataColumn();     dcOrders_CustomerID = ds.Tables["Orders"].Columns["CustomerID"];     DataRelation drCustomer_To_Orders = new DataRelation("CustomerToOrder", 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["Orders"].Columns["OrderID"];     DataColumn dcOrderDetails_OrderID = new DataColumn();     dcOrderDetails_OrderID = ds.Tables["details"].Columns["orderID"];     DataRelation drOrder_TO_Details = new DataRelation("OrdersTodetails", dcOrder_OrderID, dcOrderDetails_OrderID);     ds.Relations.Add(drOrder_TO_Details);        DataGrid1.DataSource = ds.Tables("customers").DefaultView        //This code allows the user to edit the information in the DataGrid.        //***********************************************        ds.Tables["orders"].DefaultView.AllowEdit = false;        ds.Tables["orders"].DefaultView.AllowDelete = false;        ds.Tables["Details"].DefaultView.AllowEdit = false;        ds.Tables["details"].DefaultView.AllowDelete = false;        //***********************************************					
  5. Modify the connection string to use your SQL Server-based computer.
  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 != ""){     dataGrid1.ReadOnly = true;}else{     dataGrid1.ReadOnly = false;}					
  2. Comment out the following code:
            ds.Tables["orders"].DefaultView.AllowEdit = false;        ds.Tables["orders"].DefaultView.AllowDelete = false;        ds.Tables["Details"].DefaultView.AllowEdit = false;        ds.Tables["details"].DefaultView.AllowDelete = false;					
  3. Rebuild and run the application.
  4. 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


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
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

Article ID: 322932 - Last Review: 09/04/2003 20:54:37 - Revision: 2.2

Microsoft ADO.NET (included with the .NET Framework), Microsoft ADO.NET 1.1, Microsoft Visual C# .NET 2002 Standard Edition, Microsoft Visual C# .NET 2003 Standard Edition

  • kbhowtomaster KB322932