限制仅在DataGridView列中输入数字 [英] Limit entry of only digits in a DataGridView column
本文介绍了限制仅在DataGridView列中输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个窗口应用程序。
我想 只输入数值 以 特定列中的 999999.99 (即444.55之类的数字)的形式 在DataGridView中。
怎么办?
我正在研究Visual Studio 2010.
请帮助。
解决方案
处理OnCellValidating()以检查是否容易退出单元格时的数字单元格输入,但我认为你想完全防止非数字输入。
这更棘手 - 看看这篇文章:
DataGridView的自定义数字编辑元素 [ ^ ]
将这个datagrid事件用于按键...
private void grd_bill_master_EditingControlShowing( object sender,DataGridViewEditingControlShowingEventArgs e)
{
try
{
e .Control.KeyPress + =
new KeyPressEventHandler(Control_KeyPress);
}
//这将允许像Ex一样的值。 10.20等...
private void Control_KeyPress( object sender,KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&&!char.IsDigit(e.KeyChar)
& & e.KeyChar!= ' 。')
{
e。 Handled = true ;
}
// 只允许一个小数点
if (e.KeyChar == ' 。'
&&(sender as TextBox).Text.IndexOf('' 。')> -1)
{
e.Handled = true ;
}
}
请参阅此希望它有帮助
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating .aspx [ ^ ]
Hi,
I am creating a window application.
I want to enter only numeric values in the form of "999999.99" ( ie. numbers like 444.55 ) in a particular column in the DataGridView.
How can it be done?
I am working on Visual Studio 2010.
Please help.
解决方案
It's easy enough to handle OnCellValidating() to check for numeric cell input when exiting a cell, but I think you want to completely prevent non-numeric input.
That's more tricky - take a look at this article:
Custom Numeric Edit Elements for DataGridView[^]
use this event of datagrid for keypress...
private void grd_bill_master_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); }
// and this will allowed values like Ex. 10.20 etc...
private void Control_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; } }
Refer this hope it helps
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx[^]
这篇关于限制仅在DataGridView列中输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文