C# 仅允许在Datagridview特定列中输入数值
有没有办法自定义datagridview列以只接受数值。此外,如果用户按除数字以外的任何其他字符,则当前单元格上不得键入任何内容。是否有任何方法解决此问题使用datagridview Editingcontrolshowing。。基本上像这样C# 仅允许在Datagridview特定列中输入数值,c#,datagridview,C#,Datagridview,有没有办法自定义datagridview列以只接受数值。此外,如果用户按除数字以外的任何其他字符,则当前单元格上不得键入任何内容。是否有任何方法解决此问题使用datagridview Editingcontrolshowing。。基本上像这样 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { String
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…使用以前的解决方案,每次输入EditingControlShowing事件时,您都将在按键操作时执行的事件列表中添加按键事件。这可以通过在按键事件中设置断点来轻松检查
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;
}
}
更好的解决办法是:
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;
}
}
以及事件编号检查:
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);
}
正则表达式可以是您的密友…在这里查找“javascript过滤器输入”,例如:为了简单起见,我将您的问题分为两部分:1-是否有任何方法可以自定义datagridview列以仅接受数值:我认为这完全取决于您的gridview数据源。可能有一些方法,但需要谷歌这个。2-如果用户按除数字以外的任何其他字符,则当前单元格上不必键入任何内容:是的,当然,我们可以使用JavaScript函数允许用户只输入数字值。我猜这是winform,因此您可以尝试处理该列@V4Vendetta的编辑控件的按键事件。我想您给了我一个很好的线索我将关注它,并查看调用事件修复程序(即e.Control.KeyPress+=newkeypresseventhandler(CheckKey))时发生的非工作错误;我知道这已经晚了几年,但最好的做法是解开事件处理程序的挂钩,这样就不会有太多订阅者,例如Control.KeyPress-=订阅前的CheckKey。提示:“intRate”是只接受数值的列的ColumnIndex。因此,这个解决方案比matzone的更漂亮
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;
}
}