Validation in DataGridView using c#
hello every one,
well i have data grid view which is bind from data base.. in data grid view's columns accept only integer values not string values... i just want to make function for accepting integer or double values in columns not accepting String values . if have any idea for this function than plz let me know its too urgent . i will appreciate for it..
thnaks in Advance

Nilanka DharmadasaPosted Dec 15, 2009, 12:04 AM
Use datagridview's CellValueChanged event and write following code.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
double result = 0;
if ((dataGridView1.CurrentCell == null) || (dataGridView1.CurrentCell.Value == null))
return;
string x = dataGridView1.CurrentCell.Value.ToString();
if (!Double.TryParse(x, out result))
{
dataGridView1.CurrentCell.Value = null;
MessageBox.Show("You have entered a wrong Value!");
}
}
Naeem KhanPosted Dec 15, 2009, 3:18 AM
Kirtan PatelPosted Dec 15, 2009, 12:54 AM
let me know quickly i have your solution !!
Naeem KhanPosted Dec 14, 2009, 11:58 PM
Naeem KhanPosted Dec 14, 2009, 11:50 PM
Kirtan PatelPosted Dec 14, 2009, 11:47 PM
Here is function I coded to check if Value is Integer or not .
if it helps you then check "do you like this answer" check box :)
using System.Text.RegularExpressions;
public bool IsInteger(object value)
{
string x = value.ToString();
Regex rex = new Regex("[0-9]*");
if (rex.IsMatch(x) == true)
{
return true;
}
else
{
return false;
}
}