This is a migrated thread and some comments may be shown as answers.

Help with this Validation code

2 Answers 63 Views
Spreadsheet
This is a migrated thread and some comments may be shown as answers.
john
Top achievements
Rank 1
john asked on 23 Apr 2015, 09:01 PM

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.
               this.worksheetEditor.Worksheet.Cells[activeCellIndex].SetValue(stringValue);
               this.worksheetEditor.Worksheet.Cells[activeCellIndex].SetIsBold(true);
               GradientFill greenGradientFill = new GradientFill(GradientType.FromCenter, Color.FromRgb(255, 255, 255), Color.FromRgb(252, 231, 0));
               this.worksheetEditor.Worksheet.Cells[activeCellIndex].SetFill(greenGradientFill);
               this.worksheetEditor.Focus();
           }
       }

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 Answers, 1 is accepted

Sort by
0
Anna
Telerik team
answered on 28 Apr 2015, 10:43 AM
Hello,

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);
editor.Worksheet.Cells[activeCellIndex].SetBorders(borders);

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.

Regards,
Anna
Telerik
 

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

 
0
John
Top achievements
Rank 1
answered on 28 Apr 2015, 03:16 PM
Thank you.  This did it.  I didn't even think of the borders, I guess I got stuck in the cell layout box. 
Tags
Spreadsheet
Asked by
john
Top achievements
Rank 1
Answers by
Anna
Telerik team
John
Top achievements
Rank 1
Share this question
or