在stringgrid中输数据,怎么确保输入的每行的第一列的字符不重复? (18分)

  • 主题发起人 主题发起人 delphiflyboy
  • 开始时间 开始时间
D

delphiflyboy

Unregistered / Unconfirmed
GUEST, unregistred user!
也就是怎么样判断stringgrid1.cells[1,row]的数据有没有重复?
(row:=1 to stringgrid1.rowcount-1)
 
需要每次作循环判断
 
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
var
existsindex:integer;
begin
if Trim(Value)='' then Exit;
existsindex:=StringGrid1.Cols[ACol].indexof(Value);
if (existsindex>=0) and (existsindex<>ARow) then
begin
ShowMessage('重复');
//这里还可以加入你的其他内容,比如说恢复
end;

end;
 
好象不行呀!这个事件是什么意思?
 
stringgrid里的标准事件啊,SetEditText应该是修改里面某个单元格时触发的(修改后)。
怎么不行,我测试过的。
你的判断内容区分大小写吗?上面是区分了的。
 
哦?已经发了,呵呵:)
我的方法和hongxing_dl的一样,是可以的。

unit Unit1;

interface

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

type
TForm1 = class(TForm)
ListView1: TListView;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.RowCount := 0;
StringGrid1.ColCount := 0;
StringGrid1.RowCount := 6;
StringGrid1.ColCount := 6;
end;

procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: String);
var
iR:Integer;
begin
if Trim(Value) = '' then
Exit;
iR := StringGrid1.Cols[ACol].IndexOf(Value);
if (iR >= 0) and (iR <> ARow) then
begin
MessageDlg('同列数据不运行重复!',mtInformation,[mbOK],0);
StringGrid1.Cells[ACol,ARow] := '';
end;
end;

end.
 
可不可以不用此事件,用另外一个button1触发后检测?仍后showmessage('有重复')
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.RowCount := 0;
StringGrid1.ColCount := 0;
StringGrid1.RowCount := 6;
StringGrid1.ColCount := 6;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
iR,iExist:Integer;
begin
if StringGrid1.RowCount = 0 then
Exit;
for iR := 0 to StringGrid1.RowCount - 1 do
begin
if Trim(StringGrid1.Cells[StringGrid1.Col,iR]) = '' then
Continue;
iExist := StringGrid1.Cols[StringGrid1.Col].IndexOf(StringGrid1.Cells[StringGrid1.Col,iR]);
if (iR <> iExist) and (iExist >=0) then
begin
MessageDlg('第' + IntToStr(iR) + '行与第' + IntToStr(iExist) + '行数据相同!',mtInformation,[mbOK],0);
Exit;
end;
end;
end;

end.
 
那就只有循环来解决了:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
exists:boolean;
i,j:integer;
begin
exists:=false;
for i:=1 to stringgrid1.rowcount-1 do
begin
for j:=i+1 to stringgrid1.rowcount-1 do
if stringgrid1.Cells[1,i]=stringgrid1.Cells[1,j] then
begin
exists:=true;
break;
end;
if exists then break;
end;
if exists then ShowMessage('有重复');
end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部