Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Dbgrid problem with odd and even rows

cycordcycord Member Posts: 148
i want to color my dbgrid in odd and even rows but it dont work.. my code is... procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var
test1: Real;
RowNo, farbe: Integer;
begin
with (Sender as TDBGrid) do
begin
if (gdSelected in State) then
begin
Canvas.Brush.Color := clblue;
end
else
begin
rowno := Query1.RecNo;
test1 := (RowNo / 2) - trunc(RowNo / 2);
if test1 = 0 then
begin
farbe := clWhite
end
else
begin
farbe := clYellow;
end;
Canvas.Brush.Color := farbe;
Canvas.Font.Color := clBlack;
end;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left + 2, Rect.Top + 1, Column.Field.AsString);
end
end;

Comments

  • aqgeaqge Member Posts: 3
    : i want to color my dbgrid in odd and even rows but it dont work.. my
    : code is... procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
    : const Rect: TRect; DataCol: Integer; Column: TColumn;
    : State: TGridDrawState);
    : var
    : test1: Real;
    : RowNo, farbe: Integer;
    : begin
    : with (Sender as TDBGrid) do
    : begin
    : if (gdSelected in State) then
    : begin
    : Canvas.Brush.Color := clblue;
    : end
    : else
    : begin
    : rowno := Query1.RecNo;
    : test1 := (RowNo / 2) - trunc(RowNo / 2);
    : if test1 = 0 then
    : begin
    : farbe := clWhite
    : end
    : else
    : begin
    : farbe := clYellow;
    : end;
    : Canvas.Brush.Color := farbe;
    : Canvas.Font.Color := clBlack;
    : end;
    : Canvas.FillRect(Rect);
    : Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,
    : Column.Field.AsString);
    : end
    : end;

    Hello,
    I used to use this code , hope its working

    begin
    if (TDBGrid(sender).datasource.dataset.recno mod 2) = 0 then //odd/even
    begin
    TDBGrid(sender).canvas.brush.color := clblue;
    TDBGrid(sender).canvas.font.color := clwhite;
    TDBGrid(sender).canvas.font.style := [fsitalic];

    end
    else
    begin

    TDBGrid(sender).canvas.brush.color := clsilver;
    TDBGrid(sender).canvas.font.color := clblack;
    TDBGrid(sender).canvas.font.style := [];
    end;

    if gdselected in state then
    begin
    TDBGrid(sender).canvas.brush.color := clwhite;
    TDBGrid(sender).canvas.font.color := clred;
    TDBGrid(sender).canvas.font.style := [bold];
    end;


    //but, ussually, in DBgrid, you need to add this code
    //ad the end of the code

    DBGrid1DrawColumnCell(Sender,Rect,DataCol, Column,State);
    //in Some DBGrid not

    end;


Sign In or Register to comment.