)
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 transpose the grid data in WinForms GridGroupingControl or GridDataBoundGrid?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: June 30, 2015 |
Last Revised Date: February 11, 2020

Transpose the grid data

By default, separate property is not available to transpose the Grid, that is, displaying the row data as column data. But the Grid data can be transposed by changing the DataSource structure from row to column. This article explains how to transpose data for the GridDataBoundGrid and GridGroupingControl. For GridControl, please refer to this link: GridControl.

Solution:

The row and column data can be transposed by customizing the DataSource as follows.

For GridDataBoundGrid:

The row and column data can be retrieved by using the GridModel. In this customization, the new DataSource is created (transposed data) from these retrieved data and this DataSource is assigned to the Grid.

C#

//Creates thetemporary DataTable.
DataTable _GdcTable = new DataTable();
//Creates the columns. 
for (int i = 0; i < this.GDBC1Table.Rows.Count; i++)
{
    _GdcTable.Columns.Add("Sample" + i);
}                    
int colCount = this.gridDataBoundGrid1.Model.ColCount;
//Generates the data for the transposed Grid.
for (int c = 1; c <= colCount; c++)
{
    DataRow dr = _GdcTable.NewRow();
    for (int rowIndex = 1; rowIndex < this.GDBC1Table.Rows.Count; rowIndex++)
    {                    
       dr[rowIndex - 1] = this.gridDataBoundGrid1.Model[rowIndex, c].CellValue;
    }
  _GdcTable.Rows.Add(dr);              
}
//Assigns the DataSource.
this._GDBC2Table = _GdcTable;

 

VB

'Creates the temporary DataTable.
Dim _GdcTable As New DataTable()
'Creates the columns.
For i As Integer = 0 To Me.GDBC1Table.Rows.Count - 1
  _GdcTable.Columns.Add("Sample" & i)
Next i
Dim colCount As Integer = Me.gridDataBoundGrid1.Model.ColCount
'Generates the data for the transposed Grid.
For c As Integer = 1 To colCount
Dim dr As DataRow = _GdcTable.NewRow()
For rowIndex As Integer = 1 To Me.GDBC1Table.Rows.Count - 1
   dr(rowIndex - 1) = Me.gridDataBoundGrid1.Model(rowIndex, c).CellValue
   Next rowIndex
  _GdcTable.Rows.Add(dr)
Next c
'Assigns the DataSource.
Me._GDBC2Table = _GdcTable

For GridGroupingControl:

The row and column data can be retrieved by using the Table Records. In this customization, the new DataSource is created (transposed data) from these retrieved data and this DataSource is assigned to the Grid.

C#

//Creates the temporary DataTable.
DataTable _GdcTable = new DataTable();
//Creates the columns. 
for (int i = 0; i < this.GGC1Table.Rows.Count; i++)
{
   _GdcTable.Columns.Add("Sample" + i);
}           
//Generates the data for the transposed Grid.
foreach (GridColumnDescriptor column in this.gridGroupingControl1.TableDescriptor.Columns)
{
  DataRow dr = _GdcTable.NewRow();
  foreach (Record record in this.gridGroupingControl1.Table.Records)
  {
      dr[this.gridGroupingControl1.Table.Records.IndexOf(record)] = record.GetValue(column.Name).ToString();
  }
_GdcTable.Rows.Add(dr);
} 
//Assigns the DataSource.
this._GGC2Table = _GdcTable;

VB

'Creates the temporary DataTable.
Dim _GdcTable As New DataTable()
'Creates the columns 
For i As Integer = 0 To Me.GGC1Table.Rows.Count - 1
    _GdcTable.Columns.Add("Sample" & i)
Next i
'Generates the data for the transposed Grid.
For Each column As GridColumnDescriptor In Me.gridGroupingControl1.TableDescriptor.Columns
    Dim dr As DataRow = _GdcTable.NewRow() 
    For Each record As Record In Me.gridGroupingControl1.Table.Records
       dr(Me.gridGroupingControl1.Table.Records.IndexOf(record)) =    
       record.GetValue(column.Name).ToString()
       Next record
     _GdcTable.Rows.Add(dr)
Next column
'Assigns the DataSource.
Me._GGC2Table = _GdcTable

The following are the screenshots of the GridDataBoundGrid:

Initialize the data source

Figure 1: Initial DataSource

 

Transposed data source

Figure 2: Transposed DataSource

The following are the screenshots of the GridGroupingControl:

Initial data source

Figure 3: Initial DataSource

 

Transposed data source

Figure 4: Transposed DataSource

Samples:

GridDataBoundGrid:

C#: GDBG_Transpose_CS

VB: GDBG_Transpose_VB

 

GridGroupingControl:

C#: GGC_Transpose_CS

VB: GGC_Transpose_VB

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