Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
115
Validation Images in Cell's Appearance
posted

What I'm trying to accomplish is to easily display validation to the user.  Some conditions must be reconciled before the user can save their changes.  These are considered errors and need to display the error icon.  Other conditions need to be brought to the user's attention but don't require the user to fix them.  These are warnings and should display the warning icon.

I looked into a couple of discussions on these forums.  Discussion 1 involved creating a custom DrawFilter.  I feel that this may be more than I need.  Disussion 2 described setting the cell's Appearance.Image property.  This is an easier solution and the one that I am pursuing, but I'm running into some inconsistent behavior. 

When I this process is run, after leaving each cell it displays the last displayed image.  For example: if Row.Cell[1] raised an error and Row.Cell[2] raised a warning then all icons displayed would be the Warning icons...even for Row.Cell[1].  Also, if I was to enter and leave Row.Cell[3] it would also display the Warning icon.

My code looks like this:

private void _validate(UltraGridCell cell, bool showWarnings)
{
  string errorText = string.Empty;
  string warningText = string.Empty;

  switch (cell.Column.Key)
  {
    case Columns.Name:
      if (string.IsNullOrEmpty(cell.Value.ToString()))
     errorText = "You must have a Facility Name before saving";
      break;
    case Columns.Address:
      if (string.IsNullOrEmpty(cell.Value.ToString()) && showWarnings)
        warningText = "Address is empty";
      break;
    ....
  }

  _displayErrorOrWarning(ref cell, errorText, warningText);
}
private void _displayErrorOrWarning(ref UltraGridCell cell, string errorText, string warningText)
{
  Image image = null;
  string toolTipText = string.Empty;
  if (!string.IsNullOrEmpty(errorText))
  {
    image = imageList.Images["Error"];
    toolTipText = errorText;
  }
  else if (!string.IsNullOrEmpty(warningText))
  {
    image = imageList.Images["Warning"];
    toolTipText = warningText;
  }

  if (image != null)
  {
    cell.Appearance.Image = image;
    cell.Appearance.ImageHAlign = IW.HAlign.Right;
    cell.Appearance.ImageVAlign = IW.VAlign.Middle;
  }
}

Can anyone tell me why this is happening?  Also, if there is a better way to accomplish this I'm open to those suggestions as well.  Eventually I'm going to need to figure out how to apply ToolTips to an image, so part of me feels that this isn't the appropriate method.

Thanks in advance for the help.
-Curtis

  • 469350
    Verified Answer
    Offline posted

    Hi Curtis,

    If setting the image on the appearance of one cell is affected the image in another cell, then the only explanation I can think of is that somewhere else in your code, you are setting the Appearance property on those cells to the same object.

    You can set properties on the cell.Appearance, but you can also set the Appearance object itself.

    So, for example, you could set:

    Apearance app = new Appearance();

    cell1.Appearance = app;

    cell2.Appearance = app;

    And then if you set any properties on one of the appearances, it would apply to both:

    cell1.app.Image = myImage;


    So I would check your code for "Appearance = " or look at the events of the grid to see if you are creating appearances or assigning them. Typically, this would be done in InitializeLayout or InitializeRow.