Code for setFocusToErrorOnRow
I just received an anonymous comment to the Blogger version of the Data validation in .Net using ErrorProvider and DataSet errors post, asking for the code to setFocusToErrorOnRow. This method is designed for a ComponentOne TrueDBGrid - my code is in a descendant of the C1TrueDBGrid class and wouldn't apply to a .Net data grid. Even so, here is the code - perhaps it will be useful, and as always, suggestions are welcome!
private bool setFocusToErrorOnRow (DataRow row, int rowIndex)
{
bool errorProcessed = false;
if (row.HasErrors)
{
DataColumn[] columnsInError = row.GetColumnsInError();
if (columnsInError.Length > 0)
{
int columnIndex = 0;
// Attempt to locate column in error on grid, so that we can set focus there
foreach (C1.Win.C1TrueDBGrid.C1DisplayColumn displayColumn in this.Splits[0].DisplayColumns)
{
if (displayColumn.DataColumn.DataField == columnsInError[0].ColumnName)
{
this.Row = rowIndex;
this.Col = columnIndex;
errorProcessed = true;
break;
}
columnIndex++;
}
// If we couldn't find a column, at least set focus to the row
if (!errorProcessed)
{
this.Row = rowIndex;
errorProcessed = true;
}
}
else
{
this.Row = rowIndex;
errorProcessed = true;
}
}
return errorProcessed;
}