Not logged in - Login

Allow edit on ASPxGridView grid

To allow the rows of a grid to be edited, do the following:

  • In the designer, select the 'Enable Editing', 'Enable Inserting', and/or 'Enable Deleting' properties.
    • This will add a new column to the grid with links for 'Edit', 'New', and 'Delete'.
  • Add the events to handle the updating, inserting, and deleting of the rows.
    • To create the event stub- In the designer, select the grid control.
    • In the properties window, press the button to show the events.
    • Double-click on the 'RowDeleting', 'RowInserting', or 'RowUpdating' to create the stubs.

Important: If the events are not handled, a 'Specified method is not supported' message will be thrown back to the user. Per the DevExpress Knowledge Base article: http://www.devexpress.com/Support/Center/p/Q130171.aspx?tid=4b2d6f97-c4ae-48fc-87f6-8c5da6541e40&pid=22713479-a995-45d4-9ed4-72ffa096d83d

The "Specified Method is Not Supported" error message is shown when the ASPxGridView tries to call the Update (Insert, Delete) command of its underlying DataSource, but this command is not specified. If you cannot define this command, handle the RowUpdating (RowInserting, RowDeleting) event, update the data source manually (the e.NewValues dictionary contains input value) and finally, set the e.Cancel parameter to true and call the ASPxGridView.CancelEdit method.

The RowUpdating event should look something like the following:

protected void grdTest_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
   // get the data table
   DataTable tbl = this.SampleDataTable;

   // make sure we got some data back.
   if (tbl != null)
   {

      // select the row in the datatable that is being updated. 
      // Use this if the column appears in the grid. 
      DataRow[] rows = tbl.Select(string.Format("uuid = '{0}'", e.OldValues["uuid"]));
      // use this if the column does NOT appear in the grid. The column must be part of the key on the table.
      DataRow[] rows = tbl.Select(string.Format("uuid = '{0}'", e.Keys["uuid"]));

      // make sure we got back at least 1 row.
      if (rows.Length > 0)
      {
         // get a reference to the row.
         DataRow row = rows[0];

         // copy the data from the NewValues collection over to the data row.
         //row["lPalletNumber"] = e.NewValues["lPalletNumber"];
         //row["lWeight"] = e.NewValues["lWeight"];
         //row["lWarehouseFK"] = e.NewValues["lWarehouseFK"];
         //row["bSample"] = e.NewValues["bSample"];
         //row["lBagCount"] = e.NewValues["lBagCount"];
         //row["sPackingID"] = e.NewValues["sPackingID"];
         //row["sContainerNum"] = e.NewValues["sContainerNum"];
         //row["txtNotes"] = e.NewValues["txtNotes"];

         // call the static method in our SampleData class to do the same steps as above,
         // but in a more generic fashion.
         SampleData.CopyDataRow(e.NewValues, row);
      }
   }

   // set the return value of the canceledit property
   e.Cancel = true;

   // switch to browse mode.
   this.grdTest.CancelEdit();
}

The RowInserting event should look something like the following:

protected void grdTest_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
   // get the data table
   DataTable tbl = this.SampleDataTable;

   // make sure we got some data back.
   if (tbl != null)
   {
      // Declare / create a new row that we will add to the data table.
      DataRow rowNew = tbl.NewRow();

      // copy the data from the NewValues collection over to the data row.
      //row["lPalletNumber"] = e.NewValues["lPalletNumber"];
      //row["lWeight"] = e.NewValues["lWeight"];
      //row["lWarehouseFK"] = e.NewValues["lWarehouseFK"];
      //row["bSample"] = e.NewValues["bSample"];
      //row["lBagCount"] = e.NewValues["lBagCount"];
      //row["sPackingID"] = e.NewValues["sPackingID"];
      //row["sContainerNum"] = e.NewValues["sContainerNum"];
      //row["txtNotes"] = e.NewValues["txtNotes"];

      // call the static method in our SampleData class to do the same steps as above,
      // but in a more generic fashion.
      SampleData.CopyDataRow(e.NewValues, rowNew);

      // add the row to the datatable.
      tbl.Rows.Add(rowNew);
   }

   // set the return value of the canceledit property
   e.Cancel = true;

   // switch to browse mode.
   this.grdTest.CancelEdit();
}

The RowDeleting event should look something like the following:

protected void grdTest_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
{
   // get the data table
   DataTable tbl = this.SampleDataTable;

   // make sure we got some data back.
   if (tbl != null)
   {
      // select the row in the datatable that is being updated.
      DataRow[] rows = tbl.Select(string.Format("uuid = '{0}'", e.Values["uuid"]));

      // make sure we got back at least 1 row.
      if (rows.Length > 0)
      {
         // delete the row
         rows[0].Delete();
      }

   }

   // set the return value of the canceledit property
   e.Cancel = true;

   // switch to browse mode.
   this.grdTest.CancelEdit();
}