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 DharmadasaRank #1,875 · 794 reputationPosted Dec 14, 2009, 7:04 PM EST
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!");
}
}
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 14, 2009, 6:47 PM EST
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;
}
}
Naeem KhanRank #2,072 · 698 reputationPosted Dec 14, 2009, 6:50 PM EST
Naeem KhanRank #2,072 · 698 reputationPosted Dec 14, 2009, 6:58 PM EST
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 14, 2009, 7:54 PM EST
let me know quickly i have your solution !!
Naeem KhanRank #2,072 · 698 reputationPosted Dec 14, 2009, 10:18 PM EST