使用c#.net在数据gridview中验证数据 [英] validating data in data gridview using c#.net
问题描述
你好,嗨.美好的一天.
我有个问题.我正在使用Datagridview.我只想输入一个数字作为单元格之一.我该怎么办?什么是最好的代码示例?请帮帮我.
对于我的专栏之一,其中没有天,我想输入其唯一的数字值,它不应该允许使用字母...
救救我
谢谢.
尝试一下:
私有 无效 dataGridView1_CellValidating(对象发件人,DataGridViewCellValidatingEventArgs e) { 如果(e.ColumnIndex == dataGridView1.Columns [" ].Index)这是数字列 { int i; 如果(!int.TryParse(Convert.ToString(e.FormattedValue), out i)) { e.Cancel = true ; MessageBox.Show(" ); } }
参考详细信息:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx [ ^ ]
一种简单的方法是添加一个自定义的DataGridView
列,该列托管MaskedTextBox
.在此处 [ ^ ].
私有 无效 textBox1_KeyPress(对象发送者,KeyPressEventArgs e) { 如果(例如KeyChar > = 48 && e.KeyChar < = 57 ) { e.Handled = true ; } }
Hello and Hi. Good Day.
I have a question. Im using a Datagridview. One of my cell I want to enter only a numeric number. How can I do it or what is the best example of codes. Please Help me.
for ex one of my column contains no of days i want to enter their only numeric values it should not allow alphabets...
help me plz
Thanks.
Try this:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == dataGridView1.Columns["int1"].Index) //this is numeric column { int i; if (!int.TryParse(Convert.ToString(e.FormattedValue), out i)) { e.Cancel = true; MessageBox.Show("must be numeric"); } }
Refer details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx[^]
An easy way to do this is to add a customDataGridView
column which hosts aMaskedTextBox
. There''s an example available here[^].
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 48 && e.KeyChar <= 57) { e.Handled = true; } }
这篇关于使用c#.net在数据gridview中验证数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!