C# 限制DataGridView某个字段输入数字

C# 限制DataGridView某个字段输入数字
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
     {
         if (this.dataGridView1.CurrentCell.ColumnIndex == 4)  //指定选择的字段索引 0 1.2.3.4
         {
             TextBox tb = (TextBox)e.Control; //没指定的话就dataGridView1 内 textbox限制           
             tb.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress);  
         }
     }


private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;
            int value = (int)key;
            if ((value >= 48 && value <= 57) || value == 46 || value == 8 || value == 43 || value == 45)
                e.Handled = false;
            else
                e.Handled = true;
        }
 
 
private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != .)
    {
        e.Handled = true;
    }
}
C# 限制DataGridView某个字段输入数字

C# 限制DataGridView某个字段输入数字

上一篇:轻松获取windows10锁屏壁纸


下一篇:wince扫码功能的实现