How can I perform validation on a particular DataGridViewTextBoxColumn column in my DataGridView, so that a user is required to enter a value into it?
Asked
Active
Viewed 6,162 times
1 Answers
6
i think you are looking for datagrid view text box column validation right ? if so would you pls take a look at this link
http://www.codeproject.com/Questions/93691/Validations-inside-DataGridView-TextboxColumn.aspx
EDIT 1:
you Can use this solution but it validates only numbers ,or if you want to validate the text you can change the code..
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewTextBoxCell cell = dataGridView1[2, e.RowIndex] as DataGridViewTextBoxCell;
if (cell != null)
{
if (e.ColumnIndex == 2)
{
char[] chars = e.FormattedValue.ToString().ToCharArray();
foreach (char c in chars)
{
if (char.IsDigit(c) == false)
{
MessageBox.Show("You have to enter digits only");
e.Cancel = true;
break;
}
}
}
}
}
NOTE: this code is not tested ..

Glory Raj
- 17,397
- 27
- 100
- 203
-
@AbidAli i have edited My solution , this only validates the numerical digits only , if you want to validate the text you can change that code..... – Glory Raj Sep 30 '11 at 19:59
-
@pratapk:: It takes degits. Ok. But dot(.) is not taken – Animesh Ghosh Aug 29 '13 at 14:33