双击dbgrid,在谈出属性框中点'add all fields', 选中指定字段,在object inspector中color属性设置
怎么能够对DBgrid里面显示的数据进行上色
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState); begin if Table1.Fieldbyname(′Salary′).value<=SpinEdit1.value then DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor else DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor; DBGrid1.Canvas.FillRect(Rect); DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString); end;
这是我写的一个例子,你看看 procedure TForm15.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i,j:integer; begin i:=form15.DBGrid1.DataSource.DataSet.RecordCount; if i>0 then begin for j:=0 to i-1 do begin if form15.DBGrid1.DataSource.DataSet.FieldValues['zt']='未读' then begin datacol:=form15.DBGrid1.DataSource.DataSet.RecNo; if not form15.DBGrid1.SelectedRows.CurrentRowSelected then form15.DBGrid1.Canvas.Brush.Color:=clred; form15.DBGrid1.Canvas.Brush.Color:=clred; form15.DBGrid1.Canvas.Pen.Mode:=pmmask; form15.DBGrid1.DefaultDrawColumnCell(rect,datacol,column,state); end; end; end; end;
忘了 dbgrid属性中的option中的rowselect设置为true
在dbgrid的onDrawColumnCell中加入如下程序: if “满足变色条件” then dbgrid1.canvas.font.color:=clred; else dbgrid1.canvas.font.color:=clblue;dbgrid1.DefaultDrawColumnCell(rect,datacol,column.state);
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if column.Field.Value='條件表達式' then with DBGrid1.Canvas do begin brush.Color:=clred; font.Color:=clblue; dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state); end; end;
不知道条件表达式是怎么样写,我这样写 '基本价' 或者'jbjia' 出现以下错误 "Invalid variant type conversion"
在dbgrid的OnDrawColumnCell事件中加入代碼: if 等于特定行 then begin DBgrid1.Canvas.Brush.Color := clLime; DBgrid1.Canvas.font.Color := clBlue; end else begin DBgrid1.Canvas.Brush.Color :=claqua; DBgrid1.Canvas.font.Color :=clred; end;
选中指定字段,在object inspector中color属性设置
begin
if Table1.Fieldbyname(′Salary′).value<=SpinEdit1.value then
DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor
else
DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);
end;
procedure TForm15.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
i,j:integer;
begin
i:=form15.DBGrid1.DataSource.DataSet.RecordCount;
if i>0 then
begin
for j:=0 to i-1 do
begin
if form15.DBGrid1.DataSource.DataSet.FieldValues['zt']='未读' then
begin
datacol:=form15.DBGrid1.DataSource.DataSet.RecNo;
if not form15.DBGrid1.SelectedRows.CurrentRowSelected then
form15.DBGrid1.Canvas.Brush.Color:=clred;
form15.DBGrid1.Canvas.Brush.Color:=clred;
form15.DBGrid1.Canvas.Pen.Mode:=pmmask;
form15.DBGrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
end;
end;
end;
dbgrid属性中的option中的rowselect设置为true
if “满足变色条件” then
dbgrid1.canvas.font.color:=clred;
else dbgrid1.canvas.font.color:=clblue;dbgrid1.DefaultDrawColumnCell(rect,datacol,column.state);
不用判断,直接是对某一列 是一列的数据进行上色。zyrlxy(海妖)给解答的是整个DBGrid的数据数据都上色了,
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if column.Field.Value='條件表達式' then
with DBGrid1.Canvas do
begin
brush.Color:=clred;
font.Color:=clblue;
dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
end;
"Invalid variant type conversion"
if 等于特定行 then
begin
DBgrid1.Canvas.Brush.Color := clLime;
DBgrid1.Canvas.font.Color := clBlue;
end
else
begin
DBgrid1.Canvas.Brush.Color :=claqua;
DBgrid1.Canvas.font.Color :=clred;
end;
既然要把重要的那列选出来肯定是要通过判断的
dbgrid1.columns[0].color:=clred //第一列的颜色
http://www.kaer.com.cn/default.aspx
不用了解了,你给的就是我想得到的答案。谢谢!