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

标签 c# .net winforms telerik keypress

我有一个 RadGridView,我想阻止用户在 fifth 列中写入除 '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;
                }
            }
        }
    }

关于c# - 仅允许 RadGridView 中特定列中的特定字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13539294/

相关文章:

c# - 检查列表是否包含两个不同派生类型的元素

c# - EF 递归层次结构

c# - 服务定位器和依赖注入(inject)

.net - 如何调试使用 Microsoft Moles 框架的单元测试

c# - 是否可以在WPF日历控件中加粗日期?

c# - 如何使用数据库优先方法读取 asp.net core 中的表

.net - 在不使用来自另一个线程的 Invoke/BeginInvoke 的情况下读取表单控件值(但不更改它)是否是线程安全的

c# - 参数化查询需要未提供的参数 c#

c# - 当前单元格不能设置为datagridview中的不可见单元格

.net - 如果我使用 .mdf 文件部署应用程序,用户是否需要安装 SQL 2008 Express?