The problem is that the Grid
inself can not store errors, this is the why only maintain the icon error for the FocusedRow
, but you can implement certain interface
and store all the errors for each rows.
The name of the interface
is IDataErrorInfo
, thisp rovides the functionality to offer custom error information that a user interface can bind to.
Here's an example of using this interface :
public class MyModel : IDataErrorInfo
{
/// <summary>
/// Stores error descriptions for the properties.
/// </summary>
Hashtable propertyErrors;
/// <summary>
/// Stores an error description for the item.
/// </summary>
String fNoteError;
/// <summary>
/// Gets and sets an error for the current item
/// </summary>
internal string NoteError
{
get { return fNoteError; }
set
{
if (fNoteError != null && fNoteError == value) return;
fNoteError = value;
}
}
//Returns an error description set for the item's property
string IDataErrorInfo.this[string columnName]
{
get
{
return GetColumnError(columnName);
}
}
//Returns an error description set for the current item
string IDataErrorInfo.Error
{
get { return NoteError; }
}
public String NameOfColumnA { get; set; }
public String NameOfColumnB { get; set; }
public MyModel()
{
propertyErrors = new Hashtable { { "NameOfColumnA", "" }, { "NameOfColumnB", "" }};
fNoteError = "";
}
//Sets an error for an item's property
public void SetColumnError(string elem, string error)
{
if (propertyErrors.ContainsKey(elem))
{
if ((string)propertyErrors[elem] == error) return;
propertyErrors[elem] = error;
}
}
//Gets an error for an item's property
public string GetColumnError(string elem)
{
if (propertyErrors.ContainsKey(elem))
return (string)propertyErrors[elem];
else
return "";
}
public void ClearErrors()
{
SetColumnError("NameOfColumnA", "");
SetColumnError("NameOfColumnB", "");
NoteError = "";
}
}
The above code define a model for your Grid
with two columns which may contain errors. Then in you UserControl
class you have to do it something like this :
public partial class UsDesiredName : UserControl3
{
//Some code for your class
private void ValidateData()
{
var data = gc_view.DataSource as List<MyModel>;
foreach (var item in data)
{
item.ClearErrors();
if (condition for columnA)
{
item.NoteError = "Required fields.";
item.SetColumnError("NameOfColumnA", "Required field.");
}
if (condition for columnB)
{
item.NoteError = "Required fields.";
item.SetColumnError("NameOfColumnB", "Required field.");
}
}
}
private void gc_view_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
{
ValidateData()
}
}
I hope this can be useful to someone as it was for me.