Question

I worked with the SetColumnError method sometimes, and for my experience I know when the row loses the focus the icons dissapears.

What I want now is show the same error icons at the same time in various cells in differents row of course. Something like this :

for (int i = 0; i < view.RowCount; i++)
{ 
    /* Move the focus for setting the error  */
    view.FocusedRowHandle = i;

    if (condition)
    {
        view.SetColumnError(view.Columns["NameOfColumn"],
                                         "Message", ErrorType.Critical);            
    }
}

I tried something similar, but doesn't work , my only achievement was that the last row with the focus show the error icon in the specified cells. I want to do this with the SetColumnError, but if there is another way to do it...

Any help is appreciated.

Was it helpful?

Solution

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.

OTHER TIPS

I'm afraid the GridView.SetColumnError() can be used only for focused row.

To satisfy your requirements you have to implement it manually using the GridColumn.ImageIndex and ColumnView.Images properties, to display an icon in a column header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top