DevExpress XtraGrid checkbox check not registered unless focus changes

允我心安 提交于 2021-01-27 04:11:12

问题


We have a databound XtraGrid on our Windows form. One of the columns is a check box. The problem is as follows: when users check the checkbox and click OK button, the checkbox, while visibly checked, is not considered checked by the grid. When I do this (while looping through rows):

isAllowed = Convert.ToBoolean(viewMain.GetRowCellValue(nRowCtr, "IsAllowed"))

I get back False. BUT, if the user checks the box, and then clicks somewhere else on the form or on another row in this grid, thus taking away focus from the checked checkbox, the same code above will return True.

Any insight on how to fix this behavior would be greatly appreciated.

Workaround found: With default settings, when users click on a cell to edit it, the cell goes into edit mode, loads the editor control (in this case I have a CheckEdit repository control) and changes control's value (in this case checked state). If I click on another row, or another control, the cell then gets out of edit mode, committing the change to data item. But if I click on a button, then my change is lost. The workaround is to use the CheckEdit's CheckedChanged event to close editor:

Private Sub edCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edCheck.CheckedChanged
        gridYears.FocusedView.CloseEditor()
End Sub

回答1:


There's actually a cleaner way of doing this (it works for all RepositoryItems), detailed on the DevExpress site. The idea is to call the GridView.PostEditor method from a repository item's EditValueChanged event handler to immediately save the edited value to the grid's cell and the underlying column.




回答2:


This code in the view's CellValueChanging event handler solved the problem:

private void OnCellValueChanging(object sender, CellValueChangedEventArgs e)
{
    _gridView.SetFocusedRowCellValue(_gridView.FocusedColumn, e.Value);
}


来源:https://stackoverflow.com/questions/5277903/devexpress-xtragrid-checkbox-check-not-registered-unless-focus-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!