请问如何让搜索结果的DBGrid符合条件的背景为红色(10)

  • 主题发起人 主题发起人 aa20000
  • 开始时间 开始时间
A

aa20000

Unregistered / Unconfirmed
GUEST, unregistred user!
条件为:if DataModule2.ADOQuery1.FieldByName('S1').AsString=Edit1.Text then(要求的不是改变一列,也不是一行,而是符合条件的单元格背景为红色)
 
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);begin if DataModule2.ADOQuery1.FieldByName('S1').AsString=Edit1.Text then dbgrid1.Canvas.Brush.Color :=clteal; //clred dbgrid1.refresh;end;
 
OnDrawDataCell例子procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);var grid:TDBGrid; sName,sValue :String;begin grid:= (Sender as TDBGrid); sName := Field.DisplayName; if varIsNull(Field.Value) Then exit; sValue := Field.Value ; if (sName = 'S1') and (sValue = Edit1.Text) Then with grid.Canvas do begin brush.Color:=clYellow; FillRect(Rect); Font.Color := clRed; TextOut(rect.Left+2 ,Rect.Top+2, sValue); end;end;
 
来自:de410, 来自:lvxq,感谢两位前辈提供的方法,但按上述代码执行时候,无法运行;出现提示: Undeclared identifier :'TField'请问如何解决问题?谢谢!
 
不可能啊uses db
 
回应 [lvxq] 前辈:在uses 加入 db元素之后,可以执行,但搜索出来的数据在DBGrid单元格依然没有改变背景颜色.
 
不可能啊?中断一下看看,程序有没有被执行,然后看看那个sName.或者先去掉这句 if (sName = 'S1') and (sValue = Edit1.Text) Then看看是不是都变黄低红字了如果是你自己没有改变方法名,那我真的是无语了
 
procedure TForm6.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);var grid:TDBGrid; sName,sValue :String;begin grid:= (Sender as TDBGrid); sName := Field.DisplayName; if varIsNull(Field.Value) Then exit; sValue := Field.Value ; if (sName = 'S1') and (sValue = Edit1.Text) Then with grid.Canvas do begin brush.Color:=clYellow; FillRect(Rect); Font.Color := clRed; TextOut(rect.Left+2 ,Rect.Top+2, sValue); end;end;==================================OnDrawDataCell事件如上代码,试过全掉 if (sName = 'S1') and (sValue = Edit1.Text) Then 这句,还是没有改变单元格背景颜色.
 
那就看你的dbGrid是怎么弄的了?我这里是用个DataSource 指向ADOQuery,然后是DBGrid.DataSource 也设定如果你是自己写代码往dbgrid上赋值的话,判断条件得改。把上面代码的if varIsNull(Field.Value) Then exit;也去了吧
 
情况都是一样没有任何颜色改变.数据连接设置方面和你说的那样使用DataSource 指向ADOQuery,如果是行颜色是很容易处理,但关键是某一单独单元格背景改变,还是无法实现.
 
最后添加:grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
 
最后添加:grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);出现提示: Undeclared identifier :'DataCol'
 
哦,不好意思,这段代码是用于DrawColumnCell事件的:procedure Tform1.grdBillDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);begin grdBill.Canvas.Brush.Color := $111111; grdBill.Canvas.Font.Color := $00F22791; grdBill.DefaultDrawColumnCell(Rect, DataCol, Column, State);end;
 
你可以加入要应的条件来判断那些数据可以变色
 
按照[wind_2005]前辈提供的DrawColumnCell事件的: grdBill.Canvas.Brush.Color := $111111; grdBill.Canvas.Font.Color := $00F22791; grdBill.DefaultDrawColumnCell(Rect, DataCol, Column, State);代码执行,效果为表格全部背景颜色,而不是数据的独立单元格颜色改变的
 
来自:wind_2005, 时间:2009-6-9 22:14:57, ID:3962695 | 编辑你可以加入要应的条件来判断那些数据可以变色procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);begin with Sender as TDBGrid do begin if ADOQuery1.FieldByName('did').AsInteger = 2 then begin Canvas.Brush.Color := clBlue; end; DefaultDrawColumnCell(Rect, DataCol, Column, State); end;end;自已多试试才好,上面的代码绝对能用[:)]
 
回应[wind_2005]前辈:前辈提供代码执行效果为符合条件记录的行背景颜色变蓝,而不是数据的个别单元格背景变蓝.
 
//颜色 If SameText(Column.DisplayName, ADOQuery1kdcolor.FieldName) Then Begin //DBGridEh1.Canvas.Font.Color := ADOQuery1kdcolor.Value; DBGridEh1.Canvas.Brush.Color := ADOQuery1kdcolor.Value; End; DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
 
with Sender as TDBGrid do begin if (ADOQuery1.FieldByName('did').AsInteger = 2) and (UpperCase(Column.Field.FieldName) = 'DID') then begin Canvas.Brush.Color := clBlue; end; DefaultDrawColumnCell(Rect, DataCol, Column, State); end;
 
with Sender as TDBGrid do begin if (ADOQuery1.FieldByName('did').AsInteger = 2) and (UpperCase(Column.FieldName) = 'DID') then begin Canvas.Brush.Color := clBlue; end; DefaultDrawColumnCell(Rect, DataCol, Column, State); end;
 
后退
顶部