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

Stringgrid value validation

When I want to enter a value into a string grid cell (eg: StringGrid1.Cells[2,1]) I want to check whether that same value exists either in the same column  or in the same row. It's some kind of a validation process whereby, I would be able to assign some value to the cell if only the same value does not exist in the same column or row.

sorry for my English.

thnks
0
drnadeem
Asked:
drnadeem
  • 3
1 Solution
 
kretzschmarCommented:
well, you could use

//for same row
if stringgrid1.rows[rowindex].IndexOf('VALUE') > -1 then
  showmessage('Exists Already');

//for same column
if stringgrid1.cols[colindex].IndexOf('VALUE') > -1 then
  showmessage('Exists Already');

just as kickoff
(just clear and save your current input before validating)

not sure about syntax->just from head

meikl ;-)
0
 
kretzschmarCommented:
working sample may follow . . .
0
 
kretzschmarCommented:
a working sample

unit stringgrid_validate_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  ssave : String;
  c,r : integer;
begin
  c := stringgrid1.Col;
  r := stringgrid1.Row;
  ssave := StringGrid1.Cells[c,r];
  StringGrid1.Cells[c,r] := '';
  canselect := ((StringGrid1.Rows[stringgrid1.Row].IndexOf(ssave) = -1) and //checkrow
               (StringGrid1.Cols[stringgrid1.Col].IndexOf(ssave) = -1)) or  //checkCol
               (ssave = '');  //empty is allowed
  StringGrid1.Cells[c,r] := ssave;
  if not canselect then
    showmessage('Value already exists!');
end;

end.

hope this helps

meikl ;-)
0
 
PeterLarsenCommented:
I haven't read the entire question with comments, but if you want to search through a row or a column i guess you can use IndexOf. StringGrid1.Rows[xx].IndexOf('search text');
And again, i don't know whether this have been suggested by kretzs.. !
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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