Help with this Validation code

3 posts, 0 answers
  1. john
    john avatar
    4 posts
    Member since:
    Sep 2008

    Posted 23 Apr 2015 Link to this post

    Okay, I am pulling my hair out and would like some help with this code.  I took some code from this site to do cell validation.  I don't have the current spreadsheet control  and work will not pay for an upgrade, so I am trying to make due.  Here is the code that does the validation I got to work (from another post):



    private void ValidateCellValue(ICellValue cellValue)
               bool isValid = false;
               string stringValue = cellValue.GetResultValueAsString(CellValueFormat.GeneralFormat);
               int intValue;
               if (int.TryParse(stringValue, out intValue))
                   if (intValue >= MinIntValue && intValue <= MaxIntValue)
                       isValid = true;
               if (!isValid)
                   MessageBox.Show("Not valid!");
                   Workbook workbook = new Workbook();
                   CellIndex activeCellIndex = this.worksheetEditor.Selection.ActiveCellIndex;
                   // Be carefull: SetValue() raises the CellPropertyChanged event.
                   GradientFill greenGradientFill = new GradientFill(GradientType.FromCenter, Color.FromRgb(255, 255, 255), Color.FromRgb(252, 231, 0));

    As you can see, I can put a gradient fill behind the cell if the value is invalid.  However, when I showed this to the users, they indicated it was to distracting and they just want the outline of the cell to be colored.  So I found this code on the this site and have been trying to implement it:


    private void InitializeRectangle(Rectangle rectangle, CellLayoutBox cellBox)
               //rectangle.Fill = new SolidColorBrush(Colors.Red);
               rectangle.Stroke = new SolidColorBrush(Colors.Red);
               rectangle.StrokeThickness = 1;
               rectangle.Width = cellBox.Width;
               rectangle.Height = cellBox.Height;
               Canvas.SetLeft(rectangle, cellBox.Left);
               Canvas.SetRight(rectangle, cellBox.Top);

    I have tried to pull this code in and replace the current gradient fill but keep running into problems.  I would prefer to pass the cell to the InitializeRectangle method and have it paint the border red.  Right now the method takes in a rectangle which isn't a problem, it is passing in the cellBox, how do I get the cell box in the validation method when the value is not valid?


    Any help at all on this would be much appreciated.

  2. Anna
    Anna avatar
    114 posts

    Posted 28 Apr 2015 Link to this post


    If the only thing which concerns you in the previous method is that the gradient fill is too distracting, you can replace it with setting the borders of the cell. In this case, instead of the lines where you set the fill, you would have something similar to this:
    CellBorder border = new CellBorder(CellBorderStyle.Thick, new ThemableColor(Colors.Red));
    CellBorder emptyBorder = new CellBorder(CellBorderStyle.None, new ThemableColor(Colors.Red));
    CellBorders borders = new CellBorders(border, border, border, border, emptyBorder, emptyBorder, emptyBorder, emptyBorder);

    Still, to answer your question, if you need to get the cell layout box, you can do it using the following line:
    CellLayoutBox box = editor.GetCellLayoutBox(activeCellIndex);

    I hope this will be helpful, but please let me know if you need any further clarifications.


    See What's Next in App Development. Register for TelerikNEXT.

  3. John
    John avatar
    8 posts
    Member since:
    Feb 2012

    Posted 28 Apr 2015 in reply to Anna Link to this post

    Thank you.  This did it.  I didn't even think of the borders, I guess I got stuck in the cell layout box. 
Back to Top