4

有什么方法可以自定义 datagridview 列以仅接受数值。此外,如果用户按下除数字以外的任何其他字符,则必须在当前单元格上输入任何内容。有没有办法解决这个问题

4

4 回答 4

9
    private void gvAppSummary_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (gvAppSummary.CurrentCell.ColumnIndex == intRate)
        {
            e.Control.KeyPress += new KeyPressEventHandler(gvAppSummary_KeyPress);
        }
    }

    private void gvAppSummary_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
        {
            e.Handled = true;
        }
    }
于 2013-11-26T10:32:21.587 回答
4

使用以前的解决方案,每次输入 EditingControlShowing 事件时,您都会将 KeyPressEvent 添加到要在 KeyPress 上执行的事件的«列表»中。这可以通过在 KeyPress 事件中设置断点来轻松检查。

更好的解决方案是:

private static KeyPressEventHandler NumericCheckHandler = new KeyPressEventHandler(NumericCheck);
private void dataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGrid.CurrentCell.ColumnIndex == numericColumn.Index)
    {
        e.Control.KeyPress -= NumericCheckHandler;
        e.Control.KeyPress += NumericCheckHandler;
    }
}

和事件 NumericCheck:

private static void NumericCheck(object sender, KeyPressEventArgs e)
{
    DataGridViewTextBoxEditingControl s = sender as DataGridViewTextBoxEditingControl;
    if (s != null && (e.KeyChar == '.' || e.KeyChar == ','))
    {
        e.KeyChar = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
        e.Handled = s.Text.Contains(e.KeyChar);
    }
    else
        e.Handled = !char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar);
}
于 2015-02-11T10:09:49.093 回答
3

使用datagridview Editingcontrolshowing .. 基本这样

private void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e)    
{
String sCellName =  dataGridView1.Columns(e.ColumnIndex).Name; 
    If (UCase(sCellName) == "QUANTITY") //----change with yours
    {

        e.Control.KeyPress  += new KeyPressEventHandler(CheckKey);

     }
}

private void CheckKey(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) 
        && !char.IsDigit(e.KeyChar) 
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }   
}

您可以改进此 CheckKey ...

于 2013-06-10T07:07:41.163 回答
0
   e.Control.KeyPress -= new KeyPressEventHandler(Column18qty_KeyPress);
                if (dgvProduct.CurrentCell.ColumnIndex == 18) //dgvtxtQty
                {
                    TextBox tb = e.Control as TextBox;
                    if (tb != null)
                    {
                        tb.KeyPress += new KeyPressEventHandler(Column18qty_KeyPress);
                    }
                }
 private void Column18qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }
于 2019-06-21T07:17:42.817 回答