|
1 | | -# How to select a column in winforms datagrid? |
2 | | -This example illustrates how to select a column in winforms datagrid |
| 1 | +# How to select the entire column in WinForms DataGrid (SfDataGrid) |
| 2 | + |
| 3 | +## Selection |
| 4 | +In [WinForms DataGrid](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid) You can select the entire column in DataGrid using the [SfDataGrid.SelectCells](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.SfDataGrid.html#Syncfusion_WinForms_DataGrid_SfDataGrid_SelectCells_System_Object_Syncfusion_WinForms_DataGrid_GridColumn_System_Object_Syncfusion_WinForms_DataGrid_GridColumn_) |
| 5 | +method. You should set the [SfDataGrid.SelectionUnit](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.SfDataGrid.html#Syncfusion_WinForms_DataGrid_SfDataGrid_SelectionUnit) property as **Cell** or **Any** and the [SfDataGrid.SelectionMode](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.SfDataGrid.html#Syncfusion_WinForms_DataGrid_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](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.SfDataGrid.html#Syncfusion_WinForms_DataGrid_SfDataGrid_CellClick) event. |
| 6 | + |
| 7 | +### C# |
| 8 | + |
| 9 | +```C# |
| 10 | +public Form1() |
| 11 | +{ |
| 12 | + InitializeComponent(); |
| 13 | + this.sfDataGrid.SelectionUnit = SelectionUnit.Cell; |
| 14 | + this.sfDataGrid.SelectionMode = GridSelectionMode.Extended; |
| 15 | + this.sfDataGrid.CellClick += sfDataGrid_CellClick; |
| 16 | +} |
| 17 | + |
| 18 | +void sfDataGrid_CellClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs e) |
| 19 | +{ |
| 20 | + if (e.DataRow.RowType == RowType.HeaderRow && this.sfDataGrid.View.TopLevelGroup == null) |
| 21 | + { |
| 22 | + var firstRowDate = this.sfDataGrid.View.Records[0]; |
| 23 | + var lastRowData = this.sfDataGrid.View.Records[this.sfDataGrid.View.Records.Count - 1]; |
| 24 | + var column = e.DataColumn.GridColumn; |
| 25 | + |
| 26 | + if (firstRowDate != null && lastRowData != null) |
| 27 | + { |
| 28 | + this.sfDataGrid.ClearSelection(); |
| 29 | + this.sfDataGrid.SelectCells(firstRowDate, column, lastRowData, column); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### VB |
| 36 | + |
| 37 | +```VB |
| 38 | +Public Sub New() |
| 39 | + InitializeComponent() |
| 40 | + Dim data = New OrderInfoCollection() |
| 41 | + sfDataGrid.DataSource = data.OrdersListDetails |
| 42 | + Me.sfDataGrid.AllowSorting = False |
| 43 | + Me.sfDataGrid.SelectionUnit = SelectionUnit.Cell |
| 44 | + Me.sfDataGrid.SelectionMode = GridSelectionMode.Extended |
| 45 | + AddHandler Me.sfDataGrid.CellClick, AddressOf sfDataGrid_CellClick |
| 46 | +End Sub |
| 47 | + |
| 48 | +Private Sub sfDataGrid_CellClick(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.CellClickEventArgs) |
| 49 | + If e.DataRow.RowType = RowType.HeaderRow AndAlso Me.sfDataGrid.View.TopLevelGroup Is Nothing Then |
| 50 | + Dim firstRowDate = Me.sfDataGrid.View.Records(0) |
| 51 | + Dim lastRowData = Me.sfDataGrid.View.Records(Me.sfDataGrid.View.Records.Count - 1) |
| 52 | + Dim column = e.DataColumn.GridColumn |
| 53 | + If firstRowDate IsNot Nothing AndAlso lastRowData IsNot Nothing Then |
| 54 | + Me.sfDataGrid.ClearSelection() |
| 55 | + Me.sfDataGrid.SelectCells(firstRowDate, column, lastRowData, column) |
| 56 | + End If |
| 57 | + End If |
| 58 | +End Sub |
| 59 | +``` |
| 60 | +Take a moment to peruse the [WinForms DataGrid - Selection](https://help.syncfusion.com/windowsforms/datagrid/selection) documentation, where you can find about the selection with code examples. |
0 commit comments