Setting Styles

You can use the StyleInfo object to create a style. The style can contain settings such as borders, colors, fonts, font and color themes, padding, cell indent, and alignment.

Styles can be assigned to cells, columns, rows, sheets, and conditional formats.

You can use the StyleName property to set the style for a cell, column, or row. Use the DefaultStyle property for the sheet. You can also use the Parent property to set a style for a range of cells that may individually have different style name values set. A cell inherits all the style information from the parent style. So different cells (cells in different rows or columns) may have different named styles but have the same parent style. For example, the cells may have different text colors (set in the named style) but inherit the same background color (set in the parent style).

The cell setting will override the row which overrides the column which overrides the sheet.

Using Code

The following example applies a style to a rule.

CS
Copy Code

private void Grid_Loaded_1(object sender, RoutedEventArgs e)
        {
            gcSpreadSheet1.Sheets[0].NamedStyles.Add(new GrapeCity.Xaml.SpreadSheet.Data.StyleInfo() { Background = new SolidColorBrush(Windows.UI.Colors.Red), Name = "aaa", VerticalAlignment = GrapeCity.Xaml.SpreadSheet.Data.CellVerticalAlignment.Center });
            var style = new GrapeCity.Xaml.SpreadSheet.Data.StyleInfo();
            gcSpreadSheet1.Sheets[0].Cells[0, 0].StyleName = "aaa";
            gcSpreadSheet1.Sheets[0].Rows[7].StyleName = "aaa";
            style.Background = new SolidColorBrush(Windows.UI.Colors.Blue);
            style.BorderLeft = new GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red);
            style.BorderTop = new GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red);
            style.BorderRight = new GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red);
            style.BorderBottom = new GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red);
            //Style applied to a rule
            var rule = GrapeCity.Xaml.SpreadSheet.Data.AverageRule.Create(GrapeCity.Xaml.SpreadSheet.Data.AverageConditionType.Above, style);
            rule.Ranges = new GrapeCity.Xaml.SpreadSheet.Data.CellRange[] { new GrapeCity.Xaml.SpreadSheet.Data.CellRange(1, 0, 5, 1) };
            gcSpreadSheet1.Sheets[0].ConditionalFormats.AddRule(rule);
            gcSpreadSheet1.Sheets[0].Cells[2, 0].Value = 25;
            gcSpreadSheet1.Sheets[0].Cells[1, 0].Value = 2;         
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            gcSpreadSheet1.Sheets[0].Cells[0, 0].ResetStyleName();
        }

VB
Copy Code

Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        gcSpreadSheet1.Sheets(0).NamedStyles.Add(New GrapeCity.Xaml.SpreadSheet.Data.StyleInfo() With {.Background = New SolidColorBrush(Windows.UI.Colors.Red), .Name = "aaa", .VerticalAlignment = GrapeCity.Xaml.SpreadSheet.Data.CellVerticalAlignment.Center})
        gcSpreadSheet1.Sheets(0).Cells(0, 0).StyleName = "aaa"
        gcSpreadSheet1.Sheets(0).Rows(7).StyleName = "aaa"
        Dim style As New GrapeCity.Xaml.SpreadSheet.Data.StyleInfo()
        style.Background = New SolidColorBrush(Windows.UI.Colors.Blue)
        style.BorderLeft = New GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red)
        style.BorderTop = New GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red)
        style.BorderRight = New GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red)
        style.BorderBottom = New GrapeCity.Xaml.SpreadSheet.Data.BorderLine(Windows.UI.Colors.Red)
        'Style applied to a rule
        Dim rule = GrapeCity.Xaml.SpreadSheet.Data.AverageRule.Create(GrapeCity.Xaml.SpreadSheet.Data.AverageConditionType.Above, style)
        rule.Ranges = New GrapeCity.Xaml.SpreadSheet.Data.CellRange() {New GrapeCity.Xaml.SpreadSheet.Data.CellRange(1, 0, 5, 1)}
        gcSpreadSheet1.Sheets(0).ConditionalFormats.AddRule(rule)
        gcSpreadSheet1.Sheets(0).Cells(2, 0).Value = 25
        gcSpreadSheet1.Sheets(0).Cells(1, 0).Value = 2     
    End Sub

    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)       
        gcSpreadSheet1.Sheets(0).Cells(0, 0).ResetStyleName()
    End Sub

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.