Category / Section
How to swap rows and columns in WinForms GridControl?
2 mins read
Swap rows and columns
Rows and columns can be swapped in the GridControl by handling the virtual events QueryCellInfo, SaveCellInfo, QueryRowCount, and QueryColCount. While swapping, the actual row count is assigned to the column count in the QueryColCount handler and the actual column count is assigned to the row count in QuerRowCount handler. Here, the GridControl.Data property is used. The GridData holds StyleInfoStore objects with cell specific style properties. In the QueryCellInfo event handler, the style InfoObjects are provided in a swapped manner from the GridData and in the SaveCellInfo event handler, they are saved in the correct order to the GridData.
C#
void gridControl1_QueryCellInfo(object sender, Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventArgs e) { if(e.RowIndex >0 && e.ColIndex >0) e.Style.ModifyStyle(this.gridControl1.Data[e.ColIndex, e.RowIndex], Syncfusion.Styles.StyleModifyType.Override); } private void gridControl1_QueryRowCount(object sender, GridRowColCountEventArgs e) { //In the QueryRowCount handler. e.Count = this.gridControl1.Data.ColCount; e.Handled = true; } void gridControl1_QueryColCount(object sender, GridRowColCountEventArgs e) { //In the QueryColCount handler. e.Count = this.gridControl1.Data.RowCount; e.Handled = true; } private void gridControl1_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e) { //In the SaveCellInfo handler. this.gridControl1.Data[e.ColIndex, e.RowIndex] = e.Style.Store; }
VB
Private Sub gridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventArgs) If e.RowIndex >0 AndAlso e.ColIndex >0 Then e.Style.ModifyStyle(Me.gridControl1.Data(e.ColIndex, e.RowIndex), Syncfusion.Styles.StyleModifyType.Override) End If End Sub Private Sub gridControl1_QueryRowCount(ByVal sender As Object, ByVal e As GridRowColCountEventArgs) 'In the QueryRowCount handler. e.Count = Me.gridControl1.Data.ColCount e.Handled = True End Sub Private Sub gridControl1_QueryColCount(ByVal sender As Object, ByVal e As GridRowColCountEventArgs) 'In the QueryColCount handler. e.Count = Me.gridControl1.Data.RowCount e.Handled = True End Sub Private Sub gridControl1_SaveCellInfo(ByVal sender As Object, ByVal e As GridSaveCellInfoEventArgs) 'In the SaveCellInfo handler. Me.gridControl1.Data(e.ColIndex, e.RowIndex) = e.Style.Store End Sub
Figure 1: Before swap
Figure 2: After swap
Samples:
C#: SwapRowColumn
VB: SwapRowColumn