对于TDBGrid中显示的数据,某一个格中的数据为特定值时,能否显示特定的颜色或空白(100分)

  • 主题发起人 主题发起人 beij
  • 开始时间 开始时间
B

beij

Unregistered / Unconfirmed
GUEST, unregistred user!
对于TDBGrid中显示的数据,某一个格中的数据为特定值时,能否显示特定的颜色或空白。<br>对于整行显示特定颜色可以做到,如何做到只改变特定格的颜色?或者特定格显示空白?
 
在DBGrid1构件OnDrawDataCell事件编写代码试试:<br>  procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;Field: TField; State: TGridDrawState);<br>  begin<br>&nbsp;    if Table1.Fieldbyname(′Salary′).value&lt;=SpinEdit1.value then<br>&nbsp; &nbsp; &nbsp;   DBGrid1.Canvas.Brush.Color:=ColorGrid1.ForeGroundColor<br>   else<br>&nbsp; &nbsp; &nbsp;   DBGrid1.Canvas.Brush.Color:=ColorGrid1.BackGroundColor;<br>&nbsp;    DBGrid1.Canvas.FillRect(Rect);<br>&nbsp;    DBGrid1.Canvas.TextOut(Rect.left+2,Rect.top+2,Field.AsString);<br>  end;
 
OnDBGridDrawColumnCell 事件<br><br>&nbsp;if (TDBGrid(Sender).DataSource.DataSet.RecNo mod 2=0 ) then<br>&nbsp; &nbsp; &nbsp;TDBGrid(Sender).Canvas.Brush.Color := clCream;<br>&nbsp; if (Rect.Top = TDBGrid(Sender).CellRect(TDBGrid(Sender).Col,<br>&nbsp; &nbsp; TDBGrid(Sender).Row).Top) then<br>&nbsp; &nbsp; TDBGrid(Sender).Canvas.Brush.Color := clSkyBlue;<br>&nbsp; &nbsp; TDBGrid(Sender).DefaultDrawColumnCell(Rect,DataCol,Column,State); //这一句一定不能少,不然乱套了
 
这个问题早 就有人问过啊,
 
szhcracker,wyb_506:谢谢两位的回答,你们的方法好象都不行,简单的说DBGrid中的数据有两列,“姓名”和“数量”,我的想法是如果数量为0,那么0所在的方格显示为空白或者红色。
 
设置Dbgrid的DBGrid1DrawDataCell事件中添加如下代码:<br>dbgrid1.Canvas.Font.Color:=clgreen;<br>&nbsp; if ... then<br>&nbsp; &nbsp; SetBkColor(dbgrid1.canvas.handle,clred)<br>&nbsp; else<br>&nbsp; &nbsp; setbkcolor(dbgrid1.canvas.handle,clwhite);<br>dbgrid1.Canvas.TextRect(rect,0,0,field.AsString);<br>dbgrid1.Canvas.Textout(rect.Left,rect.Top,field.AsString);
 
实在不行你就换AdvStringGrid吧,比DBGrid强大多了,设置某个单元格的颜色可以这样:<br>gdAdvStringGrid.Colors[0, 2] := clYellow;//设置第一列、第三行的单元格为黄色
 
怎么还不行呢 &nbsp;你自己研究过了没 我现在天天这样用<br>我可以专门发一张截图给你 代码就是上面的
 
to wyb_506,颜色已经搞定,只用了其中的部分:<br>&nbsp; &nbsp; TDBGrid(Sender).Canvas.Brush.Color := clSkyBlue;<br>&nbsp; &nbsp; TDBGrid(Sender).DefaultDrawColumnCell(Rect,DataCol,Column,State); //这一句一定不能少,不然乱套了 &nbsp;<br>如何让数量为0的显示空白呢?
 
1,DBGrid1DrawColumnCell事件<br>2,把里面的field[2]改成你的"数量"[fieldbyname("数量")],我用"&gt;10"你改为"=0"<br>&nbsp; &nbsp;with &nbsp; TDBGrid(sender) &nbsp; do<br>&nbsp; &nbsp; &nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; ((state=[gdselected]) &nbsp; &nbsp; or &nbsp; (state=[gdselected,gdfocused])) &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Font.Color:=clyellow;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clnavy;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp;DataSource.DataSet.RecNo &nbsp; mod &nbsp; 2&lt;&gt;0 &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clwhite<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clred;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; (Column.FieldName = Fields[2].fieldName) and (DataSource.DataSet.Fields[2].AsInteger&gt;10) &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clred<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clwhite;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultdrawcolumncell(rect,datacol,column,state); &nbsp; <br>&nbsp; &nbsp; &nbsp; end;
 
to hbklove8:谢谢回复,这样确实能够显示空白,但是一旦鼠标选中那个格,那么还是显示0
 
把里面有FONT的行删掉.
 
其实只要这样写就可以了,我多写了几句是给你参考的.<br>&nbsp; &nbsp;with &nbsp; TDBGrid(sender) &nbsp; do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; ((state=[gdselected]) &nbsp; &nbsp; or &nbsp; (state=[gdselected,gdfocused])) &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Font.Color:=clblack;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clnavy;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp; (Column.FieldName = '数量') and (DataSource.DataSet.Fieldbyname('数量').AsInteger=0) &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clred<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.Brush.Color:=clwhite;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultdrawcolumncell(rect,datacol,column,state);<br>&nbsp; &nbsp; &nbsp; end;
 
搞定,结贴。谢谢各位
 
后退
顶部