[英]Delphi / C++ Builder - Set active/selected row color in TDBGrid
I want to set the background color of the active/selected row in a TDBGrid control. 我想在TDBGrid控件中设置活动/选定行的背景色。
Using the OnDrawColumnCell event: 使用OnDrawColumnCell事件:
1) The following code will work if DBGrid has the option dgMultiSelect, if not, nothing happens: 1)如果DBGrid具有dgMultiSelect选项,则以下代码将起作用,如果没有,则什么也不会发生:
if ( grid->SelectedRows->CurrentRowSelected ) {
grid->Canvas->Brush->Color = clBlue;
}
2) The following code will work if DBGrid has the option dgRowSelect, if not, only the selected CELL, not the entire row, will be colored: 2)如果DBGrid具有dgRowSelect选项,则下面的代码将起作用,否则,将仅对选定的CELL而不是整个行进行着色:
if ( State.Contains(gdSelected) ) {
grid->Canvas->Brush->Color = clBlue;
}
How could I color the entire active/selected row without using dgRowSelect or dgMultiSelect? 如何在不使用dgRowSelect或dgMultiSelect的情况下为整个活动/选定的行着色?
From OnDrawColumnCell : 从OnDrawColumnCell :
An OnDrawColumnCell event handler can call the DefaultDrawColumnCell method to instruct the data-aware grid to write the data value in the cell.
OnDrawColumnCell事件处理程序可以调用DefaultDrawColumnCell方法,以指示数据感知网格将数据值写入单元格中。
Use DefaultDrawColumnCell like this. 像这样使用DefaultDrawColumnCell 。 This is Delphi code but you may convert it easy.
这是Delphi代码,但您可以轻松地将其转换。
procedure TForm1.DBGridDrawColumnCell(Sender: TObject;const Rect: TRect;
DataCol: Integer; Column: TColumnEh;State: TGridDrawState);
begin
.....
DBGrid.Canvas.Brush.Color := clBlue;
DBGrid.DefaultDrawColumnCell(Rect,DataCol,Column,State);
....
Update 更新资料
How to paint the DBGrid active row, without setting dgRowSelect or dgMultiSelect. 如何在不设置dgRowSelect或dgMultiSelect的情况下绘制DBGrid活动行。
Define a class that inheritant of TDBGrid to make CellRect, Col and Row public: 定义一个继承TDBGrid的类以使CellRect,Col和Row公开:
type
TMyDBGrid = class(TDBGrid)
public
function CellRect(ACol, ARow: Longint): TRect;
property Col;
property Row;
end;
function TMyDBGrid.CellRect(ACol, ARow: Longint): TRect;
begin
Result := inherited CellRect(ACol, ARow);
end;
Now we can check the top of current cell in OnDrawColumnCell event : 现在我们可以检查OnDrawColumnCell事件中当前单元格的顶部:
procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var Col,Row : Integer;
begin
col := TMyDbGrid(DBGrid1).Col;
row := TMyDbGrid(DBGrid1).Row;
if (Rect.Top = TMyDBGrid(DBGrid1).CellRect(Col,Row).Top) and
(not (gdFocused in State) or not Focused) then
DBGrid1.Canvas.Brush.Color := clBlue;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.