var i: Integer; strList: TStrings; begin //添加 for i:= 1 to self.StringGrid1.RowCount do begin StringGrid1.Cells[1,i]:= IntToStr(i); end; //查找 strList:= self.StringGrid1.Cols[1]; if strList.IndexOf('2')> 0 then showmessage('2 exists'); end;
凑个热闹,写个循环的procedure TForm1.BitBtn1Click(Sender: TObject); begin with StringGrid1 do begin if IfExistInfo then begin MessageBox(handle,PChar('已经存在数据'+trim(Edit2.Text)),'提示',MB_ICONERROR); Exit; end; Cells[1,RowCount-1] := Edit1.text; Cells[2,RowCount-1] := Edit2.text; RowCount := RowCount + 1; end; end;function TForm1.IfExistInfo: Boolean; var i: integer; begin Result := false; for i:=0 to StringGrid1.RowCount-1 do begin if Edit2.Text=StringGrid1.Cells[2,i] then begin Result := true; break; end; end; end;
i: Integer;
strList: TStrings;
begin
//添加
for i:= 1 to self.StringGrid1.RowCount do
begin
StringGrid1.Cells[1,i]:= IntToStr(i);
end; //查找
strList:= self.StringGrid1.Cols[1];
if strList.IndexOf('2')> 0 then showmessage('2 exists');
end;
=======================================================
bool __fastcall Tfm_in::CheckGrid(TStringGrid *tmp_sg)
{
AnsiString pid[2],lno[2],bno[2],bz[2];
int i,j;
//==========
for(i=1;i<tmp_sg->RowCount-1;i++)
{
pid[0]=tmp_sg->Cells[1][i].Trim(); //编号
lno[0]=tmp_sg->Cells[7][i].Trim(); //料位号
bno[0]=tmp_sg->Cells[8][i].Trim(); //批号
bz[0]=tmp_sg->Cells[0][i].Trim(); //标识
for(j=1;j<tmp_sg->RowCount-1;j++)
{
if(i!=j)//不查自身
{
pid[1]=tmp_sg->Cells[1][j].Trim(); //编号
lno[1]=tmp_sg->Cells[7][j].Trim(); //料位号
bno[1]=tmp_sg->Cells[8][j].Trim(); //批号
bz[1]=tmp_sg->Cells[0][j].Trim(); //标识
if(pid[0]==pid[1]&&lno[0]==lno[1]&&bno[0]==bno[1])
{
ShowError("标识["+bz[0]+"]和标枳["+bz[1]+"]的编号,料位号和批号都相同.");
return false;
}
} //end if
}//end for j
tmp_sg->Cells[0][i]=IntToStr(i); //重新编号
}//end for i
return true;
}
不過說實在的 使用dbgrid不是很好嗎
DBGRID不需控件,只要使用.有点像手动挡跟自动档的区别.
begin
with StringGrid1 do
begin
if IfExistInfo then
begin
MessageBox(handle,PChar('已经存在数据'+trim(Edit2.Text)),'提示',MB_ICONERROR);
Exit;
end;
Cells[1,RowCount-1] := Edit1.text;
Cells[2,RowCount-1] := Edit2.text;
RowCount := RowCount + 1;
end;
end;function TForm1.IfExistInfo: Boolean;
var
i: integer;
begin
Result := false;
for i:=0 to StringGrid1.RowCount-1 do
begin
if Edit2.Text=StringGrid1.Cells[2,i] then
begin
Result := true;
break;
end;
end;
end;