2012-11-24 52 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