Skip to content

SyncfusionExamples/how-to-select-a-column-in-winforms-datagrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Selection

In WinForms DataGrid (SfDataGrid) 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

Take a moment to peruse the WinForms DataGrid - Selection documentation, where you can find about the selection with code examples.

About

This example illustrates how to select a column in winforms datagrid

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6

Languages