2012-11-24 44 views
1

我有一個RadGridView,我想阻止用戶在第五列列中寫入'c'或'd'以外的任何字符或數字或字母。 我曾嘗試下面的代碼,但它沒有工作...只允許特定的字母在RadGridView中的特定列

private void radGridView1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (radGridView1.CurrentColumn.Index == 4) 
    { 
     if (e.KeyChar != 'c' || e.KeyChar != 'd') 
      e.Handled = true; 
    } 
} 

回答

4

使用下面的代碼片段,如果你願意做任何事情更多,例如提醒用戶,或添加驗證錯誤,這就是向上你:

 private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e) 
    { 
     String[] Acceptable = new string[] {"c", "d"}; 

     if (e.Value != null && e.ColumnIndex == 4) 
     { 
      if(e.Value != e.OldValue) 
      { 
       if (!Acceptable.Contains(e.Value)) 
       { 
        e.Cancel = true; 
       } 
      } 
     } 
    } 
+0

感謝很多:) – fadd

+0

@fadddd如果它的工作,可以接受通過單擊複選標記的答案,讓未來的用戶知道它的工作對你旁邊的答案:) – KreepN