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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to select the entire column in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid
Tags: selection

Selection

You can select the entire column in DataGrid using the SfDataGrid.SelectCells method. You should set the SfDataGrid.SelectionUnit property as Cell or Any and the SfDataGrid.SelectionMode property as Extended or Multiple for selecting the entire column. This column selection can be performed when clicking the column header using the SfDataGrid.CellClick event.

C#

public Form1()
{
    InitializeComponent();
    this.sfDataGrid.SelectionUnit = SelectionUnit.Cell;
    this.sfDataGrid.SelectionMode = GridSelectionMode.Extended;
    this.sfDataGrid.CellClick += sfDataGrid_CellClick;
}
 
void sfDataGrid_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e)
{
    if (e.DataRow.RowType == RowType.HeaderRow && this.sfDataGrid.View.TopLevelGroup == null)
    {
        var firstRowDate = this.sfDataGrid.View.Records[0];
        var lastRowData = this.sfDataGrid.View.Records[this.sfDataGrid.View.Records.Count - 1];
        var column = e.DataColumn.GridColumn;
 
        if (firstRowDate != null && lastRowData != null)
        {
            this.sfDataGrid.ClearSelection();
            this.sfDataGrid.SelectCells(firstRowDate, column, lastRowData, column);
        }
    }
}

VB

Public Sub New()
    InitializeComponent()
    Dim data = New OrderInfoCollection()
    sfDataGrid.DataSource = data.OrdersListDetails
    Me.sfDataGrid.AllowSorting = False
    Me.sfDataGrid.SelectionUnit = SelectionUnit.Cell
    Me.sfDataGrid.SelectionMode = GridSelectionMode.Extended
    AddHandler Me.sfDataGrid.CellClick, AddressOf sfDataGrid_CellClick
End Sub
 
Private Sub sfDataGrid_CellClick(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs)
    If e.DataRow.RowType = RowType.HeaderRow AndAlso Me.sfDataGrid.View.TopLevelGroup Is Nothing Then
       Dim firstRowDate = Me.sfDataGrid.View.Records(0)
       Dim lastRowData = Me.sfDataGrid.View.Records(Me.sfDataGrid.View.Records.Count - 1)
       Dim column = e.DataColumn.GridColumn
       If firstRowDate IsNot Nothing AndAlso lastRowData IsNot Nothing Then
          Me.sfDataGrid.ClearSelection()
          Me.sfDataGrid.SelectCells(firstRowDate, column, lastRowData, column)
       End If
    End If
End Sub

 

Show the entire column is selected in grid

Sample: WF

Reference link: https://help.syncfusion.com/windowsforms/datagrid/selection

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile