Search code examples
devexpressdevexpress-windows-ui

Custom Cell Appearance in GridView change event


I am disabling a checkbox column in a XtraGrid GridView with the following code (works as expected). Got the code from this post https://www.devexpress.com/Support/Center/Question/Details/Q423605:

Disabled checkbox

    private void GridViewWeeklyPlan_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
    {
        if (e.Column.FieldName == "Ignore")
        {
            CheckEditViewInfo viewInfo = ((GridCellInfo)e.Cell).ViewInfo as CheckEditViewInfo;
            viewInfo.CheckInfo.State = DevExpress.Utils.Drawing.ObjectState.Disabled;
        }
    }

ISSUE

I want to enable the checkbox again when a certain column changes and has a value. This is where I am stuck and I thought I can change it in the GridView's CellValueChanged event, but I do not know how to reference the cell/column for the row:

Change the checkbox when column has value

    private void GridViewWeeklyPlan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
        if (e.Column.FieldName != "Reason") return;


        if (String.IsNullOrEmpty(e.Value.ToString()))
        {
            //Make sure the checkbox is disabled again
        }
        else
        {
            //Enable the checkbox to allow user to select it
        }

    }

Solution

  • You need to refresh a cell in the Ignore column. You can do this by calling the GridView.RefreshRowCell method. To identify a row that you need to refresh, the CellValueChanged event provides the e.RowHandle parameter.

    private void GridViewWeeklyPlan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName != "Reason") return;
            GridView view = (GridView)sender;   
            view.RefreshRowCell(e.RowHandle, view.Columns["Ignore"]);
    
        }
    

    The CustomDrawCell event will be raised again to update the cell appearance.