Skip to main content
 首页 » 编程设计

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

2024年02月20日952php

我有一个 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; 
                } 
            } 
        } 
    }