How to select the entire column in WinForms DataGrid?
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.
Conclusion
I hope you enjoyed learning about how to select the entire column in DataGrid.
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations and WinForms DataGrid 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!