gpt4 book ai didi

delphi - 搜索数据后,使DBGrid单元格改变颜色

转载 作者:行者123 更新时间:2023-12-02 13:24:06 25 4
gpt4 key购买 nike

我正在构建一个函数:在 DBGrid 中搜索一个值后,它可以计算有多少值并标记这些单元格(更改单元格的颜色)。但我只使列更改颜色。我该如何在一个过程中执行此操作?

procedure TfmMain.N_SearchClick(Sender: TObject);

var
searchname : String;
i: integer;
frmSearch : Tfrmsearch;
data_count : integer;

begin

searchData := '';
searchRow:=self.DBGrid.DataSource.DataSet.RecNo;
searchname:= DBGrid.DataSource.DataSet.fieldbyname('FIELD').AsString;
frmSearch:=Tfrmsearch.Create(self);

try

frmSearch.ShowModal;
frmSearch.Visible:=true;

for i := 0 to DBGrid.FieldCount do
begin


if ((Pos(searchData,DBGrid.DataSource.DataSet.Fields[i].AsString)>0) AND (CompareStr(searchData,'') <> 0)
AND (Pos('VALUE_',DBGrid.DataSource.DataSet.Fields[i].FieldName)>0)
AND (searchRow=DBGrid.DataSource.DataSet.RecNo)) then
begin

data_count:=data_count+1;
//DBGrid[DBGrid.Row,i-2].Color:=clLime;

end;
end;

showmessage('countTotal: '+IntToStr(data_count));

finally
frmSearch.Free;
end;
end;

最佳答案

您所需要的只是在网格的 OnDrawColumnCell 事件中执行以下操作:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
// Change font- and grid-color if showing search results
if ShowingSearchResult then begin
TDBGrid(Sender).Canvas.Brush.Color := clSilver;
TDBGrid(Sender).Canvas.Font.Color := clBlue;
end;
TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

您可以在OnDrawColumnCell中做什么事件有点有限,但对于这种情况应该足够了。 Iirc,在某个时候 OnDrawDataCell添加以提供更大的灵 active 。

当然,您可以声明 ShowingSearchResult ( bool )变量作为 TfrmSearch 的字段并将其设置为 True在你的N_SearchClick程序。但是,由于该过程以模态方式显示表单,然后释放它,因此您可以完全省略该变量,因此 DBGrid1DrawColumnCell可能只是

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
TDBGrid(Sender).Canvas.Brush.Color := clSilver;
TDBGrid(Sender).Canvas.Font.Color := clBlue;
TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

关于delphi - 搜索数据后,使DBGrid单元格改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33431168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com