Jump to content

Hello everyone, may I ask how to set the value of a single cell in UniDBGrid using code?


zqdl8

Recommended Posts

47 minutes ago, zqdl8 said:

Hello everyone, may I ask how to set the value of a single cell in UniDBGrid using code?

Thank you very much

Hello,

At the position on the UniDBGrid;

1. Not recommended method :

MyDataSet.Edit;
  MyDataSet.FieldByName('myField_1').AsString:='Hello';
MyDataSet.Post;

 

2. Recommended Method 

Use Transaction (as Unigui is a mult-Users application) :

var Trans: TIBCTransaction;
    MyQuery:TIBCSQL;

begin

  Trans := TIBCTransaction.Create(NIL);
  try
    Trans.DefaultConnection := C_Current;
    Trans.IsolationLevel := iblReadCommitted;
    Trans.StartTransaction;
    Screen.Cursor := crHourGlass;

    //========================     
      MyQuery:=TIBCSQL.Create(NIL);
      try
         
         MyQuery.Connection:=C_Current;
         MyQuery.Transaction:=Trans;

         txtSQL:='Update MyTable Set ' +
                 ' MyField_1 = :MyField_1' +
                 ' Where (ID = :ID)';

          MyQuery.SQL.Clear;
          MyQuery.SQL.Add(txtSQL);
          if not MyQuery.Prepared then MyQuery.Prepare;

         With MyQuery do
         begin
           ParamByName('MyField_1').DataType :=ftWideString;
           ParamByName('MyField_1').AsWideString:='Hello';

           ParamByName('ID').DataType :=ftinteger;
           ParamByName('ID').AsInteger :=MyDataSet.RecNo;

         end;
         MyQuery.Execute;


      finally
        MyQuery.Free;
      end;
 

    //========================
    Trans.Commit;
    Screen.Cursor := crDefault;

    Except
      on E: Exception do
      begin
        erreur:=True;
        Trans.Rollback;
        ShowMessage(E.Message);
      end;
    end;


  finally
    Trans.Free;
  end;
end;

 

  • Like 1
Link to comment
Share on other sites

44 minutes ago, zqdl8 said:

 

Don't forget to Refresh Record :

It reqires 3 things:

          MyDataSet.RefreshOptions:=[roAfterUpdate];     
          MyDataSet.RefreshRecord;
          UniDBGrid.RefreshCurrentRow();

 

Link to comment
Share on other sites

I think that Zqdl8 needs to assign the value into CLIENT, so dataset must be update automatically.
I need the same: cell C = cell A + cell B, on the same row, at change of A or B; And I want to calculate it at Client level, so the dataset is update automatically at SERVER level on post. Otherwise too much "chatting" for a simple sum or row calculate.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...