C# 仅允许RadGridView中特定列中的特定字母

C# 仅允许RadGridView中特定列中的特定字母,c#,.net,winforms,telerik,keypress,C#,.net,Winforms,Telerik,Keypress,我有一个RadGridView,我想防止用户在第五列中写入除“c”或“d”以外的任何字符、数字或字母。 我已经尝试了下面的代码,但它没有工作 private void radGridView1_KeyPress(object sender, KeyPressEventArgs e) { if (radGridView1.CurrentColumn.Index == 4) { if (e.KeyChar != 'c' || e.KeyChar != 'd' )

我有一个RadGridView,我想防止用户在第五列中写入除“c”或“d”以外的任何字符、数字或字母。 我已经尝试了下面的代码,但它没有工作

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

如果您希望执行更多操作(如提醒用户或添加验证错误),请使用以下代码段,具体取决于您:

     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;
                }
            }
        }
    }

如果您希望执行更多操作(如提醒用户或添加验证错误),请使用以下代码段,具体取决于您:

     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;
                }
            }
        }
    }

@FADDD如果有效,您可以通过单击答案旁边的复选标记接受答案,让未来用户知道它对您有效:@FADDD如果有效,您可以通过单击答案旁边的复选标记接受答案,让未来用户知道它对您有效: