Skip to main content
 首页 » 编程设计

c#之在编辑时只允许 gridviewcell 中的数字

2024年02月20日12cyq1162

我的 C# Windows 应用程序中有一个 gridview ...它允许被编辑,我想要一个特殊的单元格(名为“Price”)只允许按键上的数字...我使用下面的代码用于 texboxes allow numbers ... 我应该在 GridView 的哪个事件中编写这段代码?

     private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e) 
    { 
        if (!char.IsDigit((char)(e.KeyChar)) && 
            e.KeyChar != ((char)(Keys.Enter)) && 
            (e.KeyChar != (char)(Keys.Delete) || e.KeyChar == Char.Parse(".")) && 
            e.KeyChar != (char)(Keys.Back)) 
        { 
            e.Handled = true; 
        } 
    } 

请您参考如下方法:

You can use CellValidating event of DataGridView. 
 
 
private void dataGridView1_CellValidating(object sender, 
        DataGridViewCellValidatingEventArgs e) 
    { 
        // Validate the Price entry. 
        if (dataGridView1.Columns[e.ColumnIndex].Name == "Price") 
        { 
        } 
    }