Hello sir,
Well i have DataGridView in windows Application i want that when i m puting a string value in Cells then given me Long Error. is it possible to make validation like they dont accept string values in Cells only accept int and decimal values if it is possible then let me know
Thanks in Advance
How to Make Validation in DataGridView Cells in c#
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Kirtan PatelRank #292 · 8.6K reputationPosted Nov 3, 2009, 10:42 AM EST
here is Code For Validating Integer Values
Check" Do you like this answer" if it helps you :)
write code like below in CelEndEdit Event of DataGridView Here I have Coded it For Checking Column 2 Cells
DataChecking You can Change it to validate any Column :)
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int RowIndex = e.RowIndex;
int columnIndex = e.ColumnIndex;
if (e.ColumnIndex == 1)
{
bool validation = true;
if (dataGridView1.Rows[RowIndex].Cells[columnIndex].Value != null && dataGridView1.Rows[RowIndex].Cells[columnIndex].Value.ToString().Trim() != "")
{
string DataToValidate = dataGridView1.Rows[RowIndex].Cells[columnIndex].Value.ToString();
foreach (char c in DataToValidate)
{
if (!char.IsDigit(c))
{
validation = false;
break;
}
}
if (validation == false)
{
dataGridView1.Rows[RowIndex].Cells[columnIndex].ErrorText = "You Can Only Enter Number here ";
dataGridView1.Rows[RowIndex].Cells[columnIndex].Value = "";
}
}
}
}