Articles in this section
Category / Section

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

2 mins read

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


Conclusion

I hope you enjoyed learning about how to select the entire column in WinForms DataGrid (Sf DataGrid).

You can refer to our Winforms DataGrid page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Winforms DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!



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