begin
With TStringGrid(Sender) do
begin
for i:=0 to RowCount-1 do
begin
lRECT:=CellRect(0,i);
if (Rect.Top>=lRECT.TOP) and (Rect.Top<=lRECT.BOTTOM) then
begin
lROW:=i;
BREAK;
end;
end;
end;
lBrushColor:=TDbGrid(Sender).Canvas.Brush.Color;
if gdSelected in State then EXIT;
if (lROW mod 2)=0 then
begin
if (DataCol mod 2)=0 then TDbGrid(Sender).Canvas.Brush.Color:=clGreen
else TDbGrid(Sender).Canvas.Brush.Color:=clBlue;
end
else
begin
TDbGrid(Sender).Canvas.Brush.Color:=TDbGrid(Sender).Color;
end;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
TDbGrid(Sender).Canvas.Brush.Color:=lBrushColor;
end;
//====================================================================
procedure TFmCtrlGrid.DBGrid1CellClick(Column: TColumn);
begin
//ShowMessage(Column.Field.AsString);
ShowMessage(IntTostr(DbGrid1.SelectedIndex));
end;
//====================================================================
procedure TFmCtrlGrid.Timer1Timer(Sender: TObject);
begin
// DbGrid Scroll 시 Refresh가 안되어서 무식하게 처리 Timer1.Tag 도용
if Timer1.Tag<>DbGrid1.DataSource.DataSet.RecNo then
begin
DbGrid1.Refresh;
Timer1.Tag:=DbGrid1.DataSource.DataSet.RecNo;
end;
end;
//====================================================================
예전에 억지로 만들었던 겁니다.
DrawDataCell을 이용하라는 내용의 자료가 많았지만 잘 안되더군요
DrawColumnCell을 이용했고 Scroll시 Color가 잘 적용되지 않아
Timer로 무식하게 처리 했습니다. 참고만 하시고
DbGrid1.DataSource.DataSet.RecNo
Column.Field.AsString (or Value)
DbGrid1.SelectedIndex 을 쓰면 RecNo와 Select된 Data를 Reference
할 수 있을겁니다.
도움이 될런지........
//====================================================================
procedure TFmCtrlGrid.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
//--------------------------------------------------------------------
var lROW,i : integer;
var lRECT : TRect;
lBrushColor: TColor;
begin
With TStringGrid(Sender) do
begin
for i:=0 to RowCount-1 do
begin
lRECT:=CellRect(0,i);
if (Rect.Top>=lRECT.TOP) and (Rect.Top<=lRECT.BOTTOM) then
begin
lROW:=i;
BREAK;
end;
end;
end;
lBrushColor:=TDbGrid(Sender).Canvas.Brush.Color;
if gdSelected in State then EXIT;
if (lROW mod 2)=0 then
begin
if (DataCol mod 2)=0 then TDbGrid(Sender).Canvas.Brush.Color:=clGreen
else TDbGrid(Sender).Canvas.Brush.Color:=clBlue;
end
else
begin
TDbGrid(Sender).Canvas.Brush.Color:=TDbGrid(Sender).Color;
end;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
TDbGrid(Sender).Canvas.Brush.Color:=lBrushColor;
end;
//====================================================================
procedure TFmCtrlGrid.DBGrid1CellClick(Column: TColumn);
begin
//ShowMessage(Column.Field.AsString);
ShowMessage(IntTostr(DbGrid1.SelectedIndex));
end;
//====================================================================
procedure TFmCtrlGrid.Timer1Timer(Sender: TObject);
begin
// DbGrid Scroll 시 Refresh가 안되어서 무식하게 처리 Timer1.Tag 도용
if Timer1.Tag<>DbGrid1.DataSource.DataSet.RecNo then
begin
DbGrid1.Refresh;
Timer1.Tag:=DbGrid1.DataSource.DataSet.RecNo;
end;
end;
//====================================================================