StringGrid1 加颜色交替的两种颜色,料单编号永远不重复的。料单编号
11110 红
11110 红
33334 绿
33334 绿
33334 绿
15310 红
61020 红
61020 红注意 后面的“红”“绿”是我加上去给个提示告诉你,还是以料单编号为准不知道怎么写代码。
主要是写代码思路,不知道你们明白我的意思啵?

解决方案 »

  1.   


    就是StringGrid表里,有料单编号。要给他们加字体颜色。上面讲得好清楚了。
      

  2.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; 
    Rect: TRect; State: TGridDrawState); 
    begin 
      with StringGrid1 do 
        if Cells[ACol,ARow] = '11110' then 
        begin 
          Canvas.Font.Color := clred; //字体颜色为红的 
          Canvas.Brush.color:=clMoneyGreen; //背景为 美元绿色 
          Canvas.FillRect(Rect); 
          Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol,ARow]); 
        end else if ................
    ..............
    end; 
      

  3.   


    啊,有一万条,不同的料单编号。都要做一万if..then吗?
      

  4.   

    StringGrid1 加颜色交替的两种颜色,料单编号永远不重复的。 料单编号 
    11110 红 
    11110 红 
    33334 绿 
    33334 绿 
    33334 绿 
    15310 红 
    61020 红 
    61020 红
    你在向stringgrid1中加载数据时,增加一个列(不显示),第一行的这列存入1,当下面一条与前一条相同是就将这个列存入1不同存入0,依次类推,这样你的这个列就只有两个值,然后在onDrawCell事件中写关于颜色的处理.
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      iR: integer;
      vCStr: String;
    begin
        setstrgrid(StringGrid1);
        for iR:=0 to Memo1.Lines.Count-1 do
        begin
          StringGrid1.cells[0,iR+1]:= IntToStr(iR);
          StringGrid1.cells[1,iR+1] := trim(Memo1.Lines[iR+1]);
          vCStr := trim(Memo1.Lines[iR+1]);
          if iR=0 then
            StringGrid1.cells[2,iR+1] := '1'
          else begin
            if vCStr = StringGrid1.cells[1,iR] then
              StringGrid1.cells[2,iR+1] := StringGrid1.cells[2,iR]
            else begin
              if StringGrid1.cells[2,iR]='0' then StringGrid1.cells[2,iR+1] := '1'
              else StringGrid1.cells[2,iR+1] := '0';
            end;
          end;
          StringGrid1.RowCount := StringGrid1.RowCount + 1;
        end;
    end;procedure TForm1.setstrgrid(strGrid:TStringGrid);
    Var
       i,j:integer;
    begin
       with strGrid do
       begin
         for i:=fixedrows to rowcount-1 do
           for j:=FixedCols  to colcount-1 do
             cells[j,i]:='';
       end;
    end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      SetGridColor(ARow,ACol,clRed,clGreen,Rect,StringGrid1);
    end;
    procedure TForm1.SetGridColor(ARow,ACol: Integer;ColorJ,ColorO: TColor; Rect: TRect;StrGrid: TStringGrid);
    var
      strValue:  String;
    begin
      with StrGrid do
      begin
       if  (ARow > 0) and (ACol > 0)  then
       begin
         if  Cells[2,ARow] = '1' then
           Canvas.Brush.Color  := ColorO
         else if Cells[2,ARow] = '0' then
           Canvas.Brush.Color  := ColorJ
         else Canvas.Brush.Color := clWhite;
         Canvas.FillRect(Rect);
       end;
       //计算显示在矩形框中的左上角位置
       strValue  :=  Cells[ACol,ARow];
       Canvas.Font  :=  Font;
       //在矩形框中写值
       Canvas.TextRect(Rect,Rect.Left+5,Rect.Top+(Rect.Bottom-Rect.Top-Canvas.TextHeight(strValue)) div 2,strValue);
      end;
    end;
      

  6.   

    你说插入'1'或是'0'我也想到了
    兄弟你的写代码好长,幸苦了。我已解决了。方法是if  StringGrid1.Cells[2,j]=StringGrid1.Cells[2,j-1] then 是‘1’ else 是'0' 。过会儿散分