• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

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

0
TTRMW
Asked:
TTRMW
  • 4
  • 4
1 Solution
 
Geert GruwezOracle dbaCommented:
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 ?
0
 
TTRMWAuthor Commented:
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
0
 
Geert GruwezOracle dbaCommented:
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 ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TTRMWAuthor Commented:
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
0
 
Geert GruwezOracle dbaCommented:
aha, that's the other way around
if you have a header in the stringgrid, (fixedrows) then the data starts at row 1

i use a function for something like that ...


function TfrmEPos.DataInGrid(const aValue: string; aGrid: TStringGrid; aColumn: integer; CaseSensitive: Boolean = False): boolean;
var I: Integer;
begin
  Result := False;
  if CaseSensitive then
    for I := 1 to aGrid.RowCount do
      if SameStr(aGrid.Cells[aColumn, I], aValue) then
      begin
        Result := True;
        Break;
      end
  else
    for I := 1 to aGrid.RowCount do
      if SameText(aGrid.Cells[aColumn, I], aValue) then
      begin
        Result := True;
        Break;
      end;
end;
 
procedure TFrmEPOS.EPOSQueryAfterScroll(DataSet: TDataSet);
begin
  btnEPosAdd.Enabled := not DataInGrid(
    VarToStr(EPOSQuery.FieldValues['ProductName']),  // Search value
    StrGridTransaction,                              // in grid
    0,                                               // in column
    True);                                           // case sensitive
end;

Open in new window

0
 
TTRMWAuthor Commented:
No header - replace I := 1 to I := 0?
0
 
Geert GruwezOracle dbaCommented:
yup
0
 
Eddie ShipmanAll-around developerCommented:
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

0
 
TTRMWAuthor Commented:
Works a treat dude, thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now