DBGrid draw

Hi everyone!

I use the OnDrawColumnCell method to draw a DBGrid in a custom way. The question is how can I determine if the cell that is being redrawn shows the field of a currect record in the datatable, as in that case I want it to be highlighted with a different color.

Ok, I can do it by comparing the column text and currect record corresponding field text, like for instance:
[code]if (Column = DBGrid.Columns[1]) and (Column.Field.Text = NotebookT.Fields[1].AsString) then ...[/code]
But in that case I'll have to have a separate check for each column to have the whole row highlighted. Is there an easier way?

Thanks for your help.

Comments

  • : Hi everyone!
    :
    : I use the OnDrawColumnCell method to draw a DBGrid in a custom way. The question is how can I determine if the cell that is being redrawn shows the field of a currect record in the datatable, as in that case I want it to be highlighted with a different color.
    :
    : Ok, I can do it by comparing the column text and currect record corresponding field text, like for instance:
    : [code]if (Column = DBGrid.Columns[1]) and (Column.Field.Text = NotebookT.Fields[1].AsString) then ...[/code]
    : But in that case I'll have to have a separate check for each column to have the whole row highlighted. Is there an easier way?
    :
    : Thanks for your help.
    :
    :
    You don't need the "Column = DBGrid.Columns[1] check. You can get the individual fields from the Columns[] property like this:
    [code]
    if DBGrid.Columns[1].Field.Text = NotebookT.Fields[1].AsString) then ...
    [/code]
    This code should do the same as the code above, but can be called for every Column.
  • [b][red]This message was edited by SaMo at 2005-3-2 12:48:30[/red][/b][hr]
    : You don't need the "Column = DBGrid.Columns[1] check. You can get the individual fields from the Columns[] property like this:
    : [code]
    : if DBGrid.Columns[1].Field.Text = NotebookT.Fields[1].AsString) then ...
    : [/code]
    : This code should do the same as the code above, but can be called for every Column.
    :

    Actually it seemed right to me first, but it doesn't work this way. The problem is that when each row is redrawn, behind the scene this record becomes current in the datatable for a short period of time (as I understand it), so the above statement actually is always true and all the rows end up being highlighted. And only after all rows have been redrawn, "truly current" record can be obtained by the above code.


  • [b][red]This message was edited by Tief at 2005-3-5 8:39:2[/red][/b][hr]
    : [b][red]This message was edited by SaMo at 2005-3-2 12:48:30[/red][/b][hr]
    : : You don't need the "Column = DBGrid.Columns[1] check. You can get the individual fields from the Columns[] property like this:
    : : [code]
    : : if DBGrid.Columns[1].Field.Text = NotebookT.Fields[1].AsString) then ...
    : : [/code]
    : : This code should do the same as the code above, but can be called for every Column.
    : :
    :
    : Actually it seemed right to me first, but it doesn't work this way. The problem is that when each row is redrawn, behind the scene this record becomes current in the datatable for a short period of time (as I understand it), so the above statement actually is always true and all the rows end up being highlighted. And only after all rows have been redrawn, "truly current" record can be obtained by the above code.
    :
    :
    :

    Well from what I understand, you want to draw the current record in a different color ?

    Many different options are available to you.

    1) Well the answer would be to keep the current record ID before the paint of DBGrid. This would enabled you to know which record is the current one.

    [code]
    if MyCurrentID = NotebookT.FieldByName('ID').AsString
    then ...
    [/code]

    2) The "Hack Solution" Would be to use the Protected Hack method in Delphi

    [code]

    uses ...

    type
    THackDBGrid = class(TCustomDBGrid)
    public
    property DataLink;
    end;

    ...

    implementation

    ...

    procedure MyForm.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
    DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
    with THackDBGrid(Sender) do begin
    if DataLink.ActiveRecord = (Row - 1)
    then Canvas.Brush.Color := clRed
    else Canvas.Brush.Color := clWhite;
    Canvas.FillRect(Rect);
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;
    end;

    [/code]


    Well, hope this Helps

    Tief.



  • : Well from what I understand, you want to draw the current record in a different color ?
    :
    : Many different options are available to you.
    :
    : 1) Well the answer would be to keep the current record ID before the paint of DBGrid. This would enabled you to know which record is the current one.
    :
    : [code]
    : if MyCurrentID = NotebookT.FieldByName('ID').AsString
    : then ...
    : [/code]
    :
    : 2) The "Hack Solution" Would be to use the Protected Hack method in Delphi
    :
    : [code]
    :
    : uses ...
    :
    : type
    : THackDBGrid = class(TCustomDBGrid)
    : public
    : property DataLink;
    : end;
    :
    : ...
    :
    : implementation
    :
    : ...
    :
    : procedure MyForm.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
    : DataCol: Integer; Column: TColumn; State: TGridDrawState);
    : begin
    : with THackDBGrid(Sender) do begin
    : if DataLink.ActiveRecord = (Row - 1)
    : then Canvas.Brush.Color := clRed
    : else Canvas.Brush.Color := clWhite;
    : Canvas.FillRect(Rect);
    : DefaultDrawColumnCell(Rect, DataCol, Column, State);
    : end;
    : end;
    :
    : [/code]
    :
    :
    : Well, hope this Helps
    :
    : Tief.
    :

    First of all thanks for your help, you code works perfectly. Still a couple of comments on it. First of all, your first proposition is actually how I made it to work myself. But it is not really convenient to have a separate variable in the application to store current record's ID and adjust it every time current record is changed. Besides, as I have several DBGrids, it really becomes annoying to have a separate variable for each and manage them correctly. So I thought there should be an easier and nicer way. The hack solution is great and really does what I need. The only comment here is that it is not really the best OOP technique to use due to obvious reasons.

    Still, these are probably the only two ways to do it the way I want, so thanks again.
Sign In or Register to comment.

Howdy, Stranger!

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

Categories