2015-04-16 69 views
0

我在這裏有一個問題。XtraGrid中的SelectionChanged或替代

我在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

你必須使用你的代碼之前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();   
} 
+0

工作完美,非常感謝你! – GeronaXx