Articles in this section
Category / Section

How to select the entire column in SfDataGrid ?

3 mins read

 

In SfDataGrid you can select the entire column by using SfDataGrid.SelectCells method. The cell selection is enabled by setting the SfDataGrid.SelectionUnit as Cell or Any and the SfDataGrid.SelectionMode as Extended or Multiple for selecting the entire column.

You can select the range of cells by passing the starting record of selection, column and ending record of selection and column as parameter to SfDataGrid.SelectCells method. In this case, pass starting and ending record in View with the column to be selected as in the following code example.

SfDataGrid.SelectCells(object startRowData, GridColumn startColumn, object endRowData, GridColumn endColumn);

 

By handling the VisualContainer.MouseLeftButtonUp event, you can click on the Header of the SfDataGrid to select a particular column by using the following code example.

using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Helpers;
using Syncfusion.UI.Xaml.ScrollAxis;
public MainWindow()
{
    InitializeComponent();
    this.sfdatagrid.Loaded += sfdatagrid_Loaded;
}
void sfdatagrid_Loaded(object sender, RoutedEventArgs e)
{
    var visualContainer = this.sfdatagrid.GetVisualContainer();
    visualContainer.MouseLeftButtonUp += visualContainer_MouseLeftButtonUp;
}
void visualContainer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 1)
    {
        //GetVisualContainer 
        var visualContainer = this.sfdatagrid.GetVisualContainer();
        var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
        var columnindex = this.sfdatagrid.ResolveToGridVisibleColumnIndex(rowcolumnindex.ColumnIndex);
        if (columnindex < 0)
            return;
        //Return if it is not HeaderRow
        if (this.sfdatagrid.GetHeaderIndex() != rowcolumnindex.RowIndex)
            return;
        var firstrowdata = this.sfdatagrid.GetRecordAtRowIndex(sfdatagrid.GetFirstRowIndex());
        //Get the record of LastRowIndex 
        var lastrowdata = this.sfdatagrid.GetRecordAtRowIndex(sfdatagrid.GetLastRowIndex());
        //Get the column of particular index
        var column = this.sfdatagrid.Columns[columnindex];
        if (firstrowdata == null || lastrowdata == null)
            return;
        //Select the column
        this.sfdatagrid.SelectCells(firstrowdata, column, lastrowdata, column);
    }
}

 

In the above code example, VisualContainer.PointToCellRowColumnIndex is used to get the RowColumnIndex of selected cell.

SfDataGrid.GetRecordAtRowIndex is a helper present in the Syncfusion.UI.Xaml.Grid.Helpers. It is used to get the record corresponding to the RowIndex.

Sample link:

WPF: http://www.syncfusion.com/downloads/support/directtrac/149035/ze/WPF-96643012933260182

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