Xtragrid SelectionChanged 或 Alternative

Xtragrid SelectionChanged or Alternative

我遇到了问题。

我在 winforms 中有一个 XtraGrid,其 multiselect 模式为真,我需要验证我正在 selecting 的行是否符合条件,select它,如果没有,deselect它。我目前正在使用这样的 SelectionChanged 方法:

private void grdProducts_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
    try
    {
        GridView view = sender as GridView;
        int[] selectedRows = view.GetSelectedRows();
        for (int i = 0; i < selectedRows.Length; i++)
        {
            if (view.IsRowSelected(selectedRows[i]))
            {
                Product product = view.GetRow(selectedRows[i]) as Candidato;
                ProcessStatus processStatus = _procesoStatusService.GetProduct(product.IdProduct);
                if (processStatus.Proccess.Inventory == (int)ProductInventory.Yes)
                {
                    view.UnselectRow(selectedRows[i]);
                    XtraMessageBox.Show("One or more products are currently in inventory.");
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}

这里的问题是当代码到达view.UnselectRow(selectedRows[i]);行时,再次调用SelectionChanged方法,程序发送多个XtraMessageBox.

有什么帮助吗?

您必须在代码后使用 BaseView.BeginSelection method before your code and BaseView.EndSelection 方法。这将阻止引发 ColumnView.SelectionChanged 事件。
这是示例:

private void grdProducts_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
{
    var view = sender as GridView;
    if (view == null) return;
    view.BeginSelection();
    try
    {
        int[] selectedRows = view.GetSelectedRows();
        for (int i = 0; i < selectedRows.Length; i++)
        {
            if (view.IsRowSelected(selectedRows[i]))
            {
                Product product = view.GetRow(selectedRows[i]) as Candidato;
                ProcessStatus processStatus = _procesoStatusService.GetProduct(product.IdProduct);
                if (processStatus.Proccess.Inventory == (int)ProductInventory.Yes)
                {
                    view.UnselectRow(selectedRows[i]);
                    XtraMessageBox.Show("One or more products are currently in inventory.");
                }
            }
        }
    }
    catch (Exception)
    {
        view.EndSelection();
        throw;
    }
    view.EndSelection();        
}