windowsforms-3027-controls-and-libraries-data-grid-examples-painting-how-to-custom-draw-cells-depending-upon-cell-values.md
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;
};
}
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Base
CustomDrawCell(gridControl1, gridView1)
Shared Sub CustomDrawCell(ByVal gridControl As GridControl, ByVal gridView As GridView)
' Handle this event to paint cells manually
AddHandler gridView.CustomDrawCell,
Sub(s, e)
If e.Column.VisibleIndex <> 2 Then
Return
End If
e.Cache.FillRectangle(Color.Salmon, e.Bounds)
e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds)
e.Handled = True
End Sub
End Sub
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:
ButtonPredefines.Glyph to display both the image and caption within the button.TextEditStyles.HideTextEditor. This hides the text box and stretches the editor’s button to fit a cell.The screenshot below shows the result:
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;
}
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Private Sub gridView1_CustomDrawCell(ByVal sender As Object, ByVal e As RowCellCustomDrawEventArgs)
If e.Column.FieldName = "Name" Then
Dim editInfo As ButtonEditViewInfo = CType(CType(e.Cell, GridCellInfo).ViewInfo, ButtonEditViewInfo)
editInfo.LeftButtons(0).Button.Caption = "Send to " & e.DisplayText
End If
End Sub
Private Sub InitButtonEdit(ByVal editor As RepositoryItemButtonEdit)
editor.Buttons(0).Kind = ButtonPredefines.Glyph
editor.Buttons(0).ImageOptions.Location = ImageLocation.MiddleLeft
editor.TextEditStyle = TextEditStyles.HideTextEditor
End Sub