如何使用dgRowSelect = False跟踪所选行 [英] how to keep track of selected rows with dgRowSelect = False

查看:154
本文介绍了如何使用dgRowSelect = False跟踪所选行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当dgRowSelect = False如何在OnDrawColumnCell方法中检测所选行?

When dgRowSelect = False how can i detect the selected row within the OnDrawColumnCell method?

不是所选单元格,而是包含所选单元格的行。 p>

Not the selected cell, but the row that contains the selected cell.

推荐答案

下面的代码似乎有效。 TDBGrid 仍然保持 SelectedRows 更新(即使它没有绘制,而没有 dgRowSelect 启用),因此您仍然可以在绘图代码中访问它们。 (您不需要启用 dgMultiSelect ,即使不需要 dgRowSelect 。)

The code below seems to work. The TDBGrid still keeps SelectedRows updated (even though it doesn't draw with them without dgRowSelect enabled), so you can still access them in your drawing code. (You do still need to enable dgMultiSelect, even though dgRowSelect is not needed.)

代码允许网格执行所有绘图,只需在所选行上设置 Canvas.Brush.Color 。如果该单元格的状态恰好是 gdSelected ,则所提供的颜色将被单个单元格的绘图代码覆盖。

The code lets the grid do all of the drawing, just setting the Canvas.Brush.Color on the selected rows. The supplied color will be overridden by the drawing code for a single cell if the state of that cell happens to be gdSelected.

我将所选行的颜色设置为 clFuchsia ,为了清晰起见,只剩下所选单元格的默认颜色(网格为丑陋 clFuchsia 选定的行,但它可以演示):

I've set the color of the selected rows to clFuchsia, and left just the selected cell the default color for clarity (the grid is ugly with clFuchsia selected rows, but it works to demonstrate):

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer;
  Column: TColumn; State: TGridDrawState);
var
  Selected: Boolean;
  Grid: TDBGrid;
begin
  Grid := TDBGrid(Sender); 
  if not (gdSelected in State) then
  begin
    Selected := Grid.SelectedRows.CurrentRowSelected;
    if Selected then
      Grid.Canvas.Brush.Color := clFuchsia;
  end;
  Grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

上面的示例结果,选择了第一和第三行:

Sample results of above, with the first and third rows selected:

您当然可以使用通常选择的颜色 clHighLight ;我发现它是混乱的,但是,因为未选择的行的当前单元格精确匹配所选行的颜色。如果他们直接相邻,那就是视觉上很烦人的。

You can, of course, just use the usual selected color of clHighLight; I found it to be confusing, though, because the current cell of an unselected row matched the color of the selected rows exactly. If they're directly next to each other, it was visually annoying.

这篇关于如何使用dgRowSelect = False跟踪所选行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆