Skip to main content Skip to footer

C1Flexgrid for LightSwitch Code Snippets

Here is another blog in our Code Snippet series. This time we'll discuss coding snippets to do the following things for C1FlexGrid for Lightswitch :

  1. Collapse Groups on Load
  2. Autosize Header Text
  3. Custom Aggregates
  4. Changing GroupedRow Column Display Text

Collapse Groups on Load

If you wish to keep all the groups in C1FlexGrid in collapsed state, then you may use the following code :

Private Sub proxy_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)  
    Dim fg = TryCast(e.Control, C1.Silverlight.FlexGrid.C1FlexGrid)  
If fg IsNot Nothing Then  
   AddHandler fg.LoadedRows, Function(s, e1)  
    For Each r In fg.Rows  
                Dim gr = TryCast(r, GroupRow)  
                If (gr IsNot Nothing) Then  
                    gr.IsCollapsed = True  
                End If  
    Next  
End Function  
End If

Autosize Header Text

To autosize the rows to the default size you may use the following code :

void \_proxy\_ControlAvailable(object sender, ControlAvailableEventArgs e)  
{  
     _flex = e.Control as C1.Silverlight.FlexGrid.C1FlexGrid;  
    _flex.LoadedRows += (s, ea) => { grid.AutoSizeFixedRows(0, 0, 1, true); };  
};

Custom Aggregates

If you want use the custom aggregates, then you need to set the GroupAggregate to Aggregate.Custom and subscribe to CustomAggregate event in LoadedRows event to get the rows values. You may use the similar code in your application. However, you will need to define your own custom aggregate logic in the CustomAggregate event :

void \_proxy\_ControlAvailable(object sender, ControlAvailableEventArgs e)  
{  
     _flex = e.Control as C1.Silverlight.FlexGrid.C1FlexGrid;  
    _flex.Columns.Where(model => model.Header == "Custom Col").Single().GroupAggregate = Aggregate.Custom;  
    \_flex.LoadedRows += (s, ea) => { \_flex.CustomAggregate += \_flex\_CustomAggregate; };  
}  

void \_flex\_CustomAggregate(object sender, CustomAggregateEventArgs e)  
{  
    decimal result = 0;  
    int r1  =e.CellRange.Row ;  
    int r2 = e.CellRange.Row2 ;  
    for(int i =r1 ; i < r2; i++)  
    {  
        result += Convert.ToDecimal(_flex[ i ,"Total"]);  
    }  
    e.Result = result;  
}

Changing GroupedRow Column Display Text

Many times you may want to show a different text for the grouped rows instead of the column name. In C1FlexGrid you can do it in two ways :

  1. If you just to change the column name, then you can simply set the name in the Display Name property for the desired column.
  2. If you want to change the text that is displayed in the grouped rows, you can do this by simply using C1FlexGrid's magical class called CellFactory. The code below simply changes the grouped column name, in our example it is Category, to a custom text 'My Category -> Categoryname (Count - no)'.
public partial class FlexibleProductsGrid  
{  
    partial void FlexibleProductsGrid_Created()  
    {  
        // Write your code here.  
        IContentItemProxy _proxy = this.FindControl("C1FlexGrid");  
        \_proxy.ControlAvailable += \_proxy_ControlAvailable;  
    }  

    void \_proxy\_ControlAvailable(object sender, ControlAvailableEventArgs e)  
    {  
            var flex = e.Control as C1FlexGrid;  
            flex.LoadedRows += (s, eaflex_LoadedRows) =>  
            { flex.GroupHeaderConverter = new MyGroupCellConverter(); };  

    }  
}  
public class MyGroupCellConverter : IValueConverter  
{  
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    {  
        var gr = parameter as GroupRow;  
        var group = gr.Group;  
        var grp = ((gr.Grid.CollectionView.GroupDescriptions[gr.Level]) as PropertyGroupDescription);  

        string desc = ((gr.Grid.CollectionView.GroupDescriptions[gr.Level]) as PropertyGroupDescription).PropertyName;  

        if (desc == "Category")  
            desc = "My Category";  

        return string.Format("{0} -> {1} (Count - {2:n0})", desc, group.Name, group.ItemCount);  

    }  

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    {  
        return null;  
    }  
}  

MESCIUS inc.

comments powered by Disqus