DHTMLX Docs & Samples Explorer

Complex updates

By default connector generates all create|update|delete queries automatically, based on configuration. But in some cases it may be necessary to define your own logic. It can be done in one of two ways

  • defining custom SQL code for operation
  • using server side events to customize operations

custom SQL code

You can define your own SQL for specific action (Insert, Update or Delete) as follows:

            dhtmlxGridConnector connector = new dhtmlxGridConnector(
                "SELECT ISO, PrintableName FROM Country",
                "UID",
                dhtmlxDatabaseAdapterType.SqlServer2005,
                ConfigurationManager.ConnectionStrings["SamplesDatabase"].ConnectionString
            );
            connector.Request.CustomSQLs.Add(CustomSQLType.Delete, "UPDATE Country SET PrintableName = '-------------' WHERE UID={UID}");
 

The parameters are the following:

  • CustomSQLType. Possible values are: “Update”, “Insert”, “Delete”
  • SQL statement. It can use fields(or their aliases) which were mentioned in connector's constructor.

custom server side events

Grid provides set of events, which can be used to handle server side action in your custom way. Those events are: BeforeSelect, BeforeInsert, BeforeUpdate

Example below adds one more field (“modify_date”) into final update statement.

      connector.BeforeUpdate += new EventHandler<DataActionProcessingEventArgs>(connector_BeforeUpdate);
 
      void connector_BeforeUpdate(object sender, DataActionProcessingEventArgs e)
      {
            e.DataAction.Data.Add("modify_date", Tools.ConvertToString(DateTime.Now));
      }

Next example adds “created_date” column into insert statement:

      connector.BeforeInsert += new EventHandler<DataActionProcessingEventArgs>(connector_BeforeInsert);
 
      void connector_BeforeInsert(object sender, DataActionProcessingEventArgs e)
      {
          e.DataAction.Data.Add("create_date", Tools.ConvertToString(DateTime.Now));
      }

Completely custom SQL:

      connector.BeforeUpdate += new EventHandler<DataActionProcessingEventArgs>(connector_BeforeUpdate);
 
      void connector_BeforeUpdate(object sender, DataActionProcessingEventArgs e)
      {
           this.Connector.Request.Adapter.ExecuteNonQuery("UPDATE ........ SET ........ WHERE ......");
           e.DataAction.SetCompleted();
      }