我在这里遇到问题。
我在winforms中有一个XtraGrid
多选模式为true,我需要验证我选择的行是否匹配条件,选择它,如果没有,则取消选择它。我目前正在使用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
。
任何帮助?
答案 0 :(得分:0)
您必须在代码之前使用BaseView.BeginSelection
方法,并在代码之后使用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();
}