DBGRID列描红

DBGRID列描红(用于特殊数据行的显示,不因RowSelect->True而改变)


 

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
  procedure Drawnumber;
  var
    TextWidth:Integer;
    TextHeigh:Integer;
  begin
      DBGrid1.Canvas.Brush.Color := clRed;
      DBGrid1.Canvas.FillRect(Rect);
      DBGrid1.Canvas.Font.Color := clWhite;
      //设置数据重画位置
      TextWidth := DBGrid1.Canvas.TextWidth(Column.Field.AsString);
      TextWidth := Rect.Left+(Rect.Right-Rect.Left)-TextWidth-3;
      TextHeigh := DBGrid1.Canvas.TextHeight(Column.Field.AsString);
      TextHeigh := Rect.Top+(Rect.Bottom-Rect.Top-TextHeigh) div 2;
      DBGrid1.Canvas.TextOut(TextWidth,TextHeigh,Column.Field.AsString);
  end;
  procedure DrawText(Field:TField;Rect:TRect);
  var
    TextWidth:Integer;
    TextHeigh:Integer;
    Leng:Integer;
  begin
      Leng := DBGrid1.Columns[2].Width+1;
      Rect.Right := Rect.Left;
      Rect.Left := Rect.Left-leng;

      DBGrid1.Canvas.Brush.Color := clRed;
      DBGrid1.Canvas.FillRect(Rect);
      DBGrid1.Canvas.Font.Color := clWhite;
      //设置数据重画位置
      TextWidth := Rect.Left+1;
      TextHeigh := DBGrid1.Canvas.TextHeight(Field.AsString);
      TextHeigh := Rect.Top+(Rect.Bottom-Rect.Top-TextHeigh) div 2;
      DBGrid1.Canvas.TextOut(TextWidth,TextHeigh,Field.AsString);
  end;
begin
  if (Drawing) and (DataCol = 3) and
        (Column.Field.DataSet.FieldByName('Subject').AsString = ComboBox1.Text)  then
  begin
    case ComboBox2.ItemIndex of
    0:begin
      if Column.Field.AsInteger > StrToInt(Edit1.Text) then
      begin
        Drawnumber;
        DrawText(Column.Field.DataSet.FieldByName('Subject'),Rect);
      end;
    end;
    1:begin
      if Column.Field.AsInteger < StrToInt(Edit1.Text) then
      begin
        Drawnumber;
        DrawText(Column.Field.DataSet.FieldByName('Subject'),Rect);
      end;
    end;
    2:begin
      if Column.Field.AsInteger = StrToInt(Edit1.Text) then
      begin
        Drawnumber;
        DrawText(Column.Field.DataSet.FieldByName('Subject'),Rect);
      end;
    end;
    end;
  end
  else
  begin
    DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  end;
end;

procedure TForm1.GetSubject;
var
  i:Integer;
begin
  with DMFrm.ADOCommand do
  begin
    Active := False;
    CommandText := 'Select Subject From StudentGrade_Tab Group By Subject';
    Active := True;
    ComboBox1.Clear;
    for i:=0 to RecordCount-1 do
    begin
      ComboBox1.Items.Add(FieldByName('Subject').AsString);
      Next;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  GetSubject;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if (ComboBox1.Text <> '') and
      (ComboBox2.Text <> '') and
      (Edit1.Text <> '') then
  begin
    Drawing := True;
    DBGrid1.Refresh;
  end;
end;


 

DBGRID列描红

你可能感兴趣的:(grid)