We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Adding column group dynamically using C#

Hi love you data grid.

I want to know
is it possible to create a data grid column group manually(dynamically) using C# and not Xaml?

I want to create a datagrid which contain a some rows within group and some of the rows without a group

would appreciate code example

thanks

1 Reply

AN Ashok N Syncfusion Team October 5, 2017 10:00 PM UTC

Hi Barak, 
 
Thanks for contacting Syncfusion support. 
 
Query regarding Groping from c# code: 
 
You perform grouping from the code by defining the GroupColumnDescription object and adding it in the SfDataGrid.GroupColumnDescriptions collection. Please refer the below UG link for more details: 
 
 
Query regarding some rows within group and some of the rows without a group:

You can achieve your requirement by using separate API and handling QueryRowHeight event. Group will be created based on value of the group property and you can hide the Group header by setting QueryRowHeightEventArgs.Height is 0. In our example we have hide the Group for zzexcludedGroup Group key, so in view those  item will be displayed without grouping.
 

Refer below code Example: 
 
// property declaration in Model 
 
string _groupProperty = "zzexcludedGroup"; 
public string GroupProperty 
{ 
    get 
    { 
        return _groupProperty; 
    } 
    set 
    { 
        this._groupProperty = value; 
        RaisePropertyChanged("GroupProperty"); 
    } 
}  
 
// Value set in OrderInfoRepository class 
if (i <= 10030) 
    ord.GroupProperty = ord.CustomerID; 
orderDetails.Add(ord);  
 
// Event handling  
 
private void DataGrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e) 
{ 
    if(dataGrid.IsCaptionSummaryRow(e.RowIndex)) 
    { 
        var captionRow = dataGrid.GetRowGenerator().Items.FirstOrDefault(rw => rw.RowIndex == e.RowIndex); 
        if(captionRow != null) 
        { 
            var group = captionRow.RowData as Group; 
            if (group != null && group.Key.ToString() == "zzexcludedGroup") 
            { 
                e.Height = 0; 
                e.Handled = true; 
            } 
        } 
    } 
}  
   

Regards,
Ashok      
 


Loader.
Up arrow icon