DBGridを一行おきに反転させる為、以前 KHE00221 さんのレスされていたものを使わせていただきました。表示画面のみのセルの移動であれば問題ないのですが、
例えばMAX10行のgridで、11行目に
'↓'、'PageDown'、スクロールバーの下方向ボタンで移動すると
Rowの数字が'-1'されているようで、正しい数値が所得できないようです。
結果交互に反転となりません。
上方向も同じ現象です。
スクロールバーによる上下動は問題ありません。
どうしたら良いのかサッパリわかりません。
どうすれば良いのでしょうか?
ソース
----------------------------------------------------
unit DBGridEx;
interface
uses
Windows, Classes, Grids, DBGrids;
type
TDBGridEx = class(TDBGrid)
private
FSaveRow : Integer;
FSaveCol : Integer;
protected
public
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);override;
published
property SaveRow : Integer read FSaveRow;
property SaveCol : Integer read FSaveCol;
end;
procedure Register;
implementation
procedure TDBGridEx.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
begin
FSaveRow := ARow;
FSaveCol := ACol;
inherited;
end;
procedure Register;
begin
RegisterComponents('Component', [TDBGridEx]);
end;
end.
------------------------------------------------
そして
procedure TForm1.DBGridEx1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var
C : TColor;
begin
if (DBGridex1.SaveRow mod 2) = 0 then C := clRed else C := clBlue;
DBGridEx1.Canvas.Brush.Color := C;
DBGridEx1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
https://www.petitmonte.com/bbs/answers?question_id=5532
ここの以下同文さんのレス参考になりますでしょうか?
TDBGridではなくTStringGridですが,
TDBGridに置き換えて縦スクロールの処理に置き換えてしまえば
できませんか?
※下記はTDBGridです
TDBGrid = class(DBGrids.TDBGrid)
private
protected
public
procedure WmKeyDown(var Msg: TMessage); Message WM_KEYDOWN;
published
end;
procedure TDBGrid.WmKeyDown(var Msg: TMessage);
begin
inherited;
with Form1.DBGrid1 do begin
case LOWORD(Msg.wParam) of
VK_DOWN, VK_UP: //ここ適当です。
begin
Invalidate;
end;
end;
end;
end;
Ruさん、ありがとうございます。
コンパイルエラーから脱却できないでいます。
継承さえよく解かっていないせいも、あるのでしょう。
私のDBGridEx1に、Ruさんのステップを加えればいいですよね?
同文さんの、新クラス宣言もTform1の、前と後ろでは
エラー内容も違いますし…
前だとSaveRowが未定義、
後ろだと実行してもKeydwnが無視さるし(ステップの左端の青い点がつかない状態)、もう少しがんばってみます。
こんな感じになるのですかね?
コンポーネントに組み込むなら
public部のWmKeyDownを組み込んでください。
その際にMessages等を組み込まないと
色々と「未定義です」と怒られるのでご注意ください。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DBGridEx, DB, DBTables, StdCtrls;
type
TDBGridEx = class(DBGridEx.TDBGridEx) //クラスの参照先は私とは違うかもしれないので調べてください
private
protected
public
procedure WmKeyDown(var Msg: TMessage); Message WM_KEYDOWN;
published
end;
TForm1 = class(TForm)
DBGridEx1: TDBGridEx;
procedure DBGridEx1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TDBGridEx }
procedure TDBGridEx.WmKeyDown(var Msg: TMessage);
begin
inherited;
with Form1.DBGridEx1 do begin
case LOWORD(Msg.wParam) of
VK_DOWN, VK_UP: //ここ適当です。
begin
Invalidate;
end;
end;
end;
end;
procedure TForm1.DBGridEx1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var
C : TColor;
begin
if (DBGridex1.SaveRow mod 2) = 0 then C := clSkyBlue else C := clWindow;
DBGridEx1.Canvas.Brush.Color := C;
DBGridEx1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end.
Ruさん、度々すみません。
このサイト、会社ではブロックされてしまい、返事が遅くなりまして御免なさい。
できました。(土曜日の朝からこんなことしてます)
クラスの定義の仕方が'あほ'だったみたいです。
有難うございました。
あのー、引続きお伺いしてもいいですか?
1レコードを2行にわたって表示したいときの方法教えて下さい。
過去ログの参照サイトが見れなかったものですから…
お手数でなければ、よろしくお願いします。
自己レスです。
'TDBCtrlGrid'を使うですね。
また、つまづきながらやってみます。
有難うございました。助かりました。
ツイート | ![]() |