tags:

views:

77

answers:

2

Hi,

I got a dbGrid with X rows. I want to update a field-value in the second row with a timer (for example, show a countdown). Thats no problem, but I want to be able to change the selected row and keep updating the second row. When the selection changes in the grid, the current record of the connected dataset changes as well, and thats a problem because the code in the timer points to the selected record.

How could that be solved? Thanks!

+4  A: 

If dataset connected to dbGrid is TClientDataSet, you can drop another TClientDataSet and clone data from grid's dataset.

Since both datasets will point to same data, you can change values in cloned dataset, and that data will show in dbGrid without tampering with dbGrids dataset.

dmajkic
+1. @doubleu If you don't know about using cloned cursors in this scenario take a look at http://www.codegearguru.com/video/049/CloneCursor.htmlIt's a good way to understand the usage of cloning.
Francis Lee
Hi and thanks fory your answers. But I have some questions regarding this problem...I got a structure like this:DataSource -> ClientDataSet and ClientDataSetClone -> DSProvider -> TQuery.Question1: When I insert new records, should I insert them via the clientDataSet (and then use applyUpdates), or should I use the TQuery directly - or doesn't it matter?Question2: When I use the clientDataSet to insert a records, this new record is always the last record in the dbgrid. How could I insert it at the top?Thanks for your help and sorry for my silly questions.
doubleu
Answer to myself :)I think I should use the clientDataSet to insert new records. After applyUpdates, I call tquery.refresh to sncronize the content between the data in the TQuery and the ClientDataSet.This also solves my second problem: When I first call tquery.refresh and then insert a new record in my clientDataSet, it's the first record in the grid.If I make obvisious mistakes, please let me know. Thanks to you guys, you and stackoverflow are awesome ;)
doubleu
A: 

Try this very straightforward approach:

if DataSource1.DataSet.State in dsEditModes then
  DataSource1.DataSet.Post; { or Cancel, depends on your needs }
try
  DataSource1.DisableControls;
  Bookmark := ClientDataSet1.GetBookmark;
  try
    if ClientDataSet1.Locate(SecondRowId, 'Id', []) then
    begin
      ClientDataSet1.Edit;
      ClientDataSet1['Counter'] := Counter;
      ClientDataSet1.Post;
    end;
    ClientDataSet1.GotoBookmark(Bookmark);
  finally
    CLientDataSet1.FreeBookmark(Bookmark);
  end;
finally
  DataSource1.EnableControls;
end;
andrius