c++buildervcltdbgrid

How do I only output an icon to a TDBGrid field?


Using C++Builder, I have a TDBGrid with a column that I want to only output an icon to. I have the icon working, but it also outputs the value. How do I disable the value from being output by default?

Here's what I do right now via the OnDrawColumnCell event:

void __fastcall TMainForm::DBGrid1DrawColumnCell(TObject *Sender, const TRect &Rect,
    int DataCol, TColumn *Column, TGridDrawState State)
{
    if (DataCol==1) {
        int iconindex=Column->Field->AsInteger;
        ImageListSmall->Draw(reinterpret_cast<TDBGrid*>(Sender)->Canvas,Rect.Left+2,Rect.Top+2,iconindex,True);
    }
}

Solution

  • You need to set the TDBGrid::DefaultDrawing property to false (that does mean, however, that you will have to draw every cell, not just icon cells. You can use the public TDBGrid::DefaultDrawColumnCell() method to draw your non-icon cells).

    The documentation for the OnDrawColumnCell event clearly says:

    If DefaultDrawing is true, the data will already be drawn in the cell before the OnDrawColumnCell event, and the grid draws a focus rectangle around selected cells after the OnDrawColumnCell event.

    And the documentation for the DefaultDrawing property says:

    Set DefaultDrawing to true to allow the data-aware grid to draw the data in the cells of the grid automatically. Set DefaultDrawing to false to turn off the default drawing when providing customized painting in an OnDrawColumnCell event handler or an OnDrawDataCell event handler.

    When DefaultDrawing is true, the data-aware grid fetches the data for each cell from the appropriate entry in the Fields array, and writes it to the cell. It then draws the focus rectangle if the cell has focus, or if the cell is selected and the Options property includes dgAlwaysShowSelection.

    When DefaultDrawing is false, the data-aware grid draws the appropriate background color on the cell and sets up the brush and font described by the appropriate TColumn object. However, the value of the field is not written to the cell, and no focus rectangle is drawn.