Articles in this section
Category / Section

How to restrict the expand and collapse the groups when click on the caption row in WPF DataGrid (SfDataGrid)?

1 min read

By default, when click anywhere in caption row group is expand or collapse in SfDataGrid. But you can change this by GroupExpanding, GroupCollapsing, and SelectionChanging events.

this.sfDataGrid.GroupCollapsing += SfDataGrid_GroupCollapsing;
this.sfDataGrid.GroupExpanding += SfDataGrid_GroupExpanding;
this.sfDataGrid.SelectionChanging += SfDataGrid_SelectionChanging;private void SfDataGrid_SelectionChanging(object sender, GridSelectionChangingEventArgs e)
{
    var visualcontainer = this.sfDataGrid.GetVisualContainer();
    var point = Mouse.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method 
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row  
    if (!rowColumnIndex.IsEmpty)
    {
        if (rowColumnIndex.ColumnIndex == 0)
            e.Cancel = true;
    }
}
 
private void SfDataGrid_GroupExpanding(object sender, GroupChangingEventArgs e)
{
    var visualcontainer = this.sfDataGrid.GetVisualContainer();
    var point = Mouse.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {               
        if (rowColumnIndex.ColumnIndex > 0)
            e.Cancel = true;
    }
}
 
private void SfDataGrid_GroupCollapsing(object sender, GroupChangingEventArgs e)
{
    var visualcontainer = this.sfDataGrid.GetVisualContainer();
    var point = Mouse.GetPosition(visualcontainer);
    //Here you can get the row and column index based on the pointer position by using PointToCellRowColumnIndex() method
    var rowColumnIndex = visualcontainer.PointToCellRowColumnIndex(point);
    //When the rowindex is zero , the row will be header row 
    if (!rowColumnIndex.IsEmpty)
    {
        if (rowColumnIndex.ColumnIndex > 0)
            e.Cancel = true;
    }
}

 

 



A gif demo for shows the process

View Samples in GitHub.


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied