DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: power71483
今日帖子: 12
在线用户: 9
导航: 论坛 -> 数据库专区 斑竹:liumazi,waterstone  
作者:
男 moom (小新啊) ▲▲▲△△ -
普通会员
2018/3/29 0:02:56
标题:
请问 ehlib 的 dbgrideh 行记录.如何某字段值相同就显示相同的行颜色? 浏览:1448
加入我的收藏
楼主: 请问 ehlib 的 dbgrideh 行记录.如何某字段值相同就显示相同的行颜色?

如: name 字段,有2行一样是 A ,则 这2行 显示 相同 行颜色...

  id   name       
  1       A   
  2       A   
  3       B   
  4       C   
  5       C   
  7       C   
  8       D     

1,2 = A = 红色 , 4,5,7 =C= 黄色...
反正就是相同的一种颜色,其它的相同就其它颜色...间隔着颜色...

注:AAA B CCC 都是 动态数据,,不是 固定值... 
 
请问如何可以实现? 谢谢...
----------------------------------------------
谢谢各位大大
作者:
男 moom (小新啊) ▲▲▲△△ -
普通会员
2018/3/29 1:09:14
1楼: 网上找了段,,,似乎效果不佳...

use FireDAC.Comp.Client,FireDAC.Comp.DataSet;

procedure TForm.DBGridEh1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
var
  ID,
  PriorID : Integer;
  DBGrid : TDBGridEh;
  PriorCellValue :String;
  CurrentCellValue :String;

  function fncCellValue(aID : Integer; aDataSet : TDataset; const aFieldName : String) : String;
  var
  fdmTable : TFDMemTable;
  begin
  fdmTable := TFDMemTable.Create(nil);
    try
    if (fdmTable.Active) then
    fdmTable.Close;

    fdmTable.CloneCursor(TFDDataSet(aDataSet), True, False);

    if fdmTable.Locate('ID', aID, []) then
    Result := fdmTable.FieldByName(aFieldName).AsString
    else
    Result := EmptyStr;

    finally
    if (fdmTable.Active) then
    fdmTable.Close;

    fdmTable.Free;
    end;
  end;


begin
DBGrid := Sender as TDBGridEh;

  with DBGrid do
  begin
  Canvas.FillRect(Rect);
  DefaultDrawColumnCell(Rect, DataCol, Column, State);

  if NOT(DataSource.DataSet.Bof and DataSource.DataSet.Eof) then
  begin
    if (gdFocused in State) or (gdSelected in State) then
    begin
          if (DataSource.DataSet.State = dsBrowse) then
          begin
          Canvas.Brush.Color := clGreen; // Color of the selected row
          Canvas.Font.Color := clWhite;
          end
          else
          if (DataSource.DataSet.State in [dsEdit, dsInsert]) then
          begin
          Canvas.Brush.Color := clWhite; // Color of the row been edited or inserted
          Canvas.Font.Color := clNavy;
          end;

    end;

        if (Column.HideDuplicates) and NOT((gdFocused in State) or (gdSelected in State)) then
        begin
        ID := DataSource.DataSet.FieldByName('ID').AsInteger;
        PriorID := Pred(ID);

        PriorCellValue := fncCellValue(PriorID, DataSource.DataSet, Column.FieldName);
        CurrentCellValue := fncCellValue(ID, DataSource.DataSet, Column.FieldName);

        {Only the first cell of the column with duplicate value has the color clMoneyGreen.}
        if (PriorCellValue = CurrentCellValue) then
        Canvas.Brush.Color := clWhite // Color of the other cells of the column with the same duplicate value as the first one.
        else
        Canvas.Brush.Color := clMoneyGreen; // Color of the first cell with the duplicate value
        end;
        end;

  Canvas.FillRect(Rect);
  DefaultDrawColumnCell(Rect, DataCol, Column, State);
  Canvas.Refresh;
  end;


end;
----------------------------------------------
谢谢各位大大
作者:
男 luckyrandom (luckyrandom) ★☆☆☆☆ -
普通会员
2018/3/29 8:37:50
2楼: 对结果集COPY一个副本,再根据当前行的值到副本里对比前后行确定颜色
----------------------------------------------
SQL SERVER DBA QQ:315054403 曾经的Delphier  缘在上海
作者:
男 luckyrandom (luckyrandom) ★☆☆☆☆ -
普通会员
2018/3/29 8:38:52
3楼: 也可以在SQL查询结果时就处理好颜色标志,将有邻近行相同值的作标记。。这样在显示颜色时逻辑就简单清晰了
----------------------------------------------
SQL SERVER DBA QQ:315054403 曾经的Delphier  缘在上海
作者:
男 moom (小新啊) ▲▲▲△△ -
普通会员
2018/3/29 10:11:34
4楼: 楼上可否举个sql 语句例子 ?  如何重复加个标记值 ?  谢谢
----------------------------------------------
谢谢各位大大
作者:
男 zhahongyi (如风) ★☆☆☆☆ -
普通会员
2018/3/29 13:27:05
5楼: select id,name,ROW_NUMBER() OVER(PARTITION BY name ORDER BY name) as xx
from 
name值相同的会产生相同的xx值,从1开始排序增加,程序里就好处理了。

procedure DBGridEh1GetCellParams(Sender: TObject; Column: TColumnEh; AFont: TFont; var Background: TColor;
  State: TGridDrawState);
begin
  if cds.FieldValues['xx'] = 1 then
    AFont.Color := clRed;
  if cds.FieldValues['xx'] = 2 then
    AFont.Color := clgreed;

end;
----------------------------------------------
-
作者:
男 moom (小新啊) ▲▲▲△△ -
普通会员
2018/3/29 14:33:16
6楼: 3q
唉..SQLITE不支持上面SQL....
继续找资料中....
----------------------------------------------
谢谢各位大大
作者:
男 moom (小新啊) ▲▲▲△△ -
普通会员
2018/4/2 23:29:50
7楼: 继续 中....
----------------------------------------------
谢谢各位大大
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行70.3125毫秒 RSS