Link to home
Start Free TrialLog in
Avatar of TTRMW
TTRMW

asked on

Checking for a duplicate in a string grid against an SQL query, in delphi?

Hi there - bit of background, after something is added to the string grid, BtnEPOSadd is disabled. This bit of code is on the afterscroll event to check whether the item exists, as a way around the problem of duplicates in the string grid...

...However it's not working! Changing the current record seems to serve only to enable the button, even if the current record is already in the string grid...

Cheers in advance :D
procedure TFrmEPOS.EPOSQueryAfterScroll(DataSet: TDataSet);
var count : integer;
begin
   for count := 0 to Strrow do
      if StrGridTransaction.Cells[0,Count] = EPOSQuery.FieldValues['ProductName']then
        begin
          BtnEPOSadd.Enabled := false;
        end
      else BtnEPOSadd.enabled := true;
end;

Open in new window

Avatar of Geert G
Geert G
Flag of Belgium image

i would assume firstly that count would go from 0 to strrow-1 ...
are you adding data to the StringGrid and displaying data in a DBGrid ?
i don't fully understand what you are looking for and where ... a screenshot ?
Avatar of TTRMW
TTRMW

ASKER

Quite possibly aye, though not having -1 shouldn't make a difference since [0, strrow] will simply be empty...?

There's a dbgrid which displays records, the dataset it displays is EPOSQuery. The idea is that a different onclick event copies two fields from the current record into this string grid, then disables the add button until the selected record is changed. Strrow is incremented for where in the strgrid is next to be written to, and is used in several procedures for deleting etc..

This bit of code operates on the afterscroll event for the dataset, and should go through the string grid, check whether any of the cells in column 0 contain the same as the value in fieldvalues['ProductName'] of the current record.

If you still need a screenshot just shout but I don't see that it'll make things much clearer! :D
you have 1 row in your stringgrid copied from the current row in the dbgrid

when changing the row in the stringgrid you want to find if the data changed in column 0 exists in the dbgrid ?

something like that ?
Avatar of TTRMW

ASKER

when changing the row in the dbgrid, want to find if the data of the current record of the dataset exists in the string grid. Close :P
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TTRMW

ASKER

No header - replace I := 1 to I := 0?
yup
Remember, the stringgrid has the rows object and you can use the IndexOf on it to determine if any certain data is in the particular row you are looking at. This way, you don't have to iterate through the COLUMNS to check each value to see if it matches.
procedure TForm1.FormCreate(Sender: TObject);
var
  I, J, K : Integer;
begin
  K := 0;
  with StringGrid1 do
    for I := 0 to ColCount - 1 do
      for J:= 0 to RowCount - 1 do
        begin
          K := K + 1;
          Cells[I,J] := 'Cell ' + IntToStr(K);
        end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to StringGrid1.RowCount-1 do
    if StringGrid1.Rows[i].IndexOf('Cell 18') > 0then
      ShowMessage('found it');
end;

Open in new window

Avatar of TTRMW

ASKER

Works a treat dude, thanks!