delphi中DBGrid里的数据满足条件的以显示红色
在DBGrid DrawDataCell事件下写:
procedure TForm2.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if Field.Index = 1 then {这里检测第几列 范围0..max}
if Field.AsString = '免费' then {检测是否免费(你要是用其他类型表示用户类型你可以改)}
begin
{这里你随便画反正有Rect}
TDBGrid(Sender).Canvas.Brush.Style:=bsClear;
TDBGrid(Sender).Canvas.Pen.Color:=clRed; {颜色}
TDBGrid(Sender).Canvas.Rectangle(Rect);
{DBGrid1}
end;
end;
//下面是既支持列高亮又支持行高亮
type
TThiefDBGrid = class(TDBGrid)
property DataLink;
end;
{DBGrid1的DrawDataCell事件}
procedure TFormMain.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if RadioButton1.Checked then
begin
if Not (TThiefDBGrid(TDBGrid(Sender)).DataLink.Fields[1{这里是用户类型列号}].AsString = ComboBox1.Text{是否等于'免费用户'}) then
Exit;
end
else
begin
if Not ((Field.Index = 1{这里是用户类型列号})and(Field.AsString = ComboBox1.Text)) then
Exit;
end;
with TDBGrid(Sender) do
begin
if not CheckBox2.Checked{这里决定字高亮还是背景高亮} then
begin
Canvas.Font.Color:= not ColorBox1.Selected;
Canvas.Brush.Color:=ColorBox1.Selected;{颜色}
end
else
begin
Canvas.Font.Color:=ColorBox1.Selected;{颜色}
end;
DefaultDrawDataCell(Rect,Field,State);
end;
end;
给你写了个例子演示(如图)
要源码的话留邮箱
广告 您可能关注的内容 |