【发布时间】:2021-09-22 18:24:58
【问题描述】:
我正在尝试创建一个包含几列的 radgridview,其中一列是电话号码,并且应该只接受数字。这是我的代码的一部分:
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
if (e.Column.Name == "column1")
{
var editor = e.ActiveEditor as RadTextBoxEditor;
var element = editor.EditorElement as RadTextBoxEditorElement;
element.TextBoxItem.KeyPress -= TextBoxItem_KeyPress;
element.TextBoxItem.KeyPress += TextBoxItem_KeyPress;
}
}
private void TextBoxItem_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
当我第一次单击 column1 中的一个单元格时,它会起作用。但是,当我想编辑一个选中的单元格(不点击)时,如果我按下任何键(甚至是非数字键),它就会接受第一个键。显然 CellEditorInitialized 在第一次按键后被触发,这是不希望的。你能帮我解决这个问题吗?
【问题讨论】:
-
这听起来像是GridViewMaskBoxColumn 的工作。