Pergunta

I have 2 columns I'm working with in an XtraGrid. When Column1's value changes, I'd like to perform some logic and possibly change the value of Column2 and disable Column2 as well. You can see my code below, but I have 3 problems:

  1. My CustomRowCellEdit function seems to run non-stop in the background.
  2. The SetRowValue on Column2 doesn't seem to really happen unless I click away from the row; I need the change to happen as soon as Column1 is changed.
  3. How can I disable within my IF block?

I've added the following Event to a Grid:

this._myGridView.CustomRowCellEdit += 
    new DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventHandler(
        this.myGridView_CustomRowCellEdit);

Here is the Event:

private void myGridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
    if (e.Column.FieldName == "Column1" && e.RowHandle >= 0)
    {
        GridView gv = sender as GridView;
        string value1 = gv.GetRowCellValue(e.RowHandle, gv.Columns["Column1"]).ToString();

        if (value1 == "something")
        {
            gv.SetRowCellValue(e.RowHandle, gv.Columns["Column2"], someOtherValue);
            // I'd like to disable Column2 in this IF logic.
        }
    }
}
Foi útil?

Solução

In the DevX docs, there is a note about the CustomRowCellEdit event that says

Due to the XtraGrid control's infrastructure, the CustomRowCellEdit event fires frequently - each time a cell is refreshed. Therefore, do not implement complex logic for your CustomRowCellEdit event handler...

Given your stated requirements, my approach would be to use the CellValueChanged event instead of CustomRowCellEdit. Your handler could then say something like

private void myGridView_CellValueChanged(object sender, CellValueChangedEventArgs e) {
    if (e.Column.FieldName != "Column1") return;
    GridView gv = sender as GridView;
    if (e.Value == "something") {
        gv.SetRowCellValue(e.RowHandle, gv.Columns["Column2"], someOtherValue);
    }
}

To make an individual cell non-editable at runtime, see this topic on the DevExpress support site. how to set readyonly for rows at runtime using Devxpress Grid Contorl. Essentially what you need to do is handle the grid view's ShowingEditor event, and using the FocusedRowHandle and FocusedColumn properties, decide whether or not to allow editing for the current cell. To disable editing, set the Cancel property of the CancelEventArgs to true.

Hope this helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top