delphitdbgrid

Best Way to Select a Single Row with TDBGrid in Delphi


I have an application that uses an SQLite database. The form has 14 different fields to display a single record for adding/editing and a TDBGrid to show all records. It appears that TDBGrid does not have an event similar to "OnRowChange" in which I could execute some code to enable/disable four of the fields based on the record type of the selected database row.

Is there a known work around or other potential solution?


Solution

  • Use the OnDataChange event of your connected Datasource. Your Grid ist only a representation of your actual pointer and data of your DataSource or DataSet.

    Here you can react on the different fieldvalues like so:

    procedure TForm1.DataSource1DataChange(Sender: TObject; 
                                          Field: TField);
    begin
      if DataSource1.DataSet.FieldByName('RecordType').AsString = 'SpecialType' then
      begin
        Edit1.Enabled := True;
        Edit2.Enabled := False;
        //...
      end
      else
      begin
        Edit1.Enabled := False;
        Edit2.Enabled := True;
        //...
      end;
      //...
    end;
    

    I would not recommend to track changes in your Data over a UI Element. Keep UI Seperated from your Logic and DB.