Friends, I’m using a datagridview control in my visual studio 2005 windows application. Here I’ve 5 columns. Among these 5, 2nd(colIndex 1) and 3rd(colIndex 2) column have text data type and 4th(colIndex 3)and 5th(colIndex 4) have double data type column. Now I’ve to check the cell value length for colIndex 1 and 2 whether the entered value length is greater than a specific(10 for colIndex 1 and 100 for colIndex 2) value? If so then I’ve to show corresponding message. Can you suggest in which datagridview event I should check the entered/typed cell value and how to check the value? I’ve used following code in CellValidating event
string columnName = dgView.Columns[e.ColumnIndex].Name;
if (dgView.Rows[e.RowIndex].Cells[dgViewColumn.TAN].Value.ToString().Length > 10)
{
e.Cancel = true;
MessageBox.Show(columnName + " must be 10 Digits Long!");
}
But it’s showing columnName of next column.
And as I’ve a feature for the grid that pressing “Enter” key will move the focus to next cell, it’s showing error as “Operation did not succeed because the program cannot commit or quit a cell value change.” in CellValidating event. Please help me.
I hope you know that the
CellValidatingevent would be triggered for each cell, so maybe you are seeing the message since it might fail for the length part of thedoubledatatype columns.So you should put a check that you are checking the first and second column respectively and then doing the checks for length.
you can also choose to use
e.FormattedValueto get the current value in the cell too.Also you can set the
MaxInputLengthfor the Textbox column in the properties too (in case you need it)