请教:
    我想在StringGrid1中的第二列不能有数据重复,如有第二次重复就马上提示数据重复?怎写?

解决方案 »

  1.   

    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;
      

  2.   

    在保存数据前,进行数据判断.就是全部有效行循环一下.然后对关键字进行对比,如果有相同就进行报错.(如果全是关键字,那么全部比较.)
    =======================================================
    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;
    }
      

  3.   

    本人基本不使用StringGrid,3樓的朋友方法應該可行
    不過說實在的 使用dbgrid不是很好嗎
      

  4.   

    DBGRID....跟STRINGGRID不是一样.噢,我是说针对问题.STRINGGRID比DBGRID好控制,明白你在控制什么.
    DBGRID不需控件,只要使用.有点像手动挡跟自动档的区别.
      

  5.   

    凑个热闹,写个循环的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;