Skip to main content

How to: Custom Draw Cells Depending Upon Cell Values

  • 2 minutes to read

The following code demonstrates how to use the CustomDrawCell event to re-paint cells that belong to the third Grid Column.

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

CustomDrawCell(gridControl1, gridView1);

public static void CustomDrawCell(GridControl gridControl, GridView gridView) {
    // Handle this event to paint cells manually
    gridView.CustomDrawCell += (s, e) => {
        if (e.Column.VisibleIndex != 2) return;
        e.Cache.FillRectangle(Color.Salmon, e.Bounds);
        e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds);
        e.Handled = true;
    };
}

How to: Display Buttons with Custom Captions within Cells

The following example demonstrates how to handle the CustomDrawCell event to display custom captions within buttons of cell editors.

In this example, the Name column’s ColumnEdit property is set to RepositoryItemButtonEdit.

The InitButtonEdit method configures the Name column’s editor as follows:

  • Sets the Kind property to ButtonPredefines.Glyph to display both the image and caption within the button.
  • Sets the editor’s TextEditStyle property to TextEditStyles.HideTextEditor. This hides the text box and stretches the editor’s button to fit a cell.

The screenshot below shows the result:

Custom Text within Edit Buttons - WinForms Data Grid | DevExpress

using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

private void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    if(e.Column.FieldName == "Name") {
        ButtonEditViewInfo editInfo = (ButtonEditViewInfo)((GridCellInfo)e.Cell).ViewInfo;
        editInfo.LeftButtons[0].Button.Caption = "Send to " + e.DisplayText;
    }
}

void InitButtonEdit(RepositoryItemButtonEdit editor) {
    editor.Buttons[0].Kind = ButtonPredefines.Glyph;
    editor.Buttons[0].ImageOptions.Location = ImageLocation.MiddleLeft;
    editor.TextEditStyle = TextEditStyles.HideTextEditor;
}