)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to swap rows and columns in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 20, 2019
Tags: swapping

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

Before swap

Figure 1: Before swap

After swap

Figure 2: After swap

Samples:

C#: SwapRowColumn

VB: SwapRowColumn

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon