How to transpose the grid data in WinForms Grid Control?
Transpose the grid data
By default, separate property is not available to transpose the WinForms GridGrouping, 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:
Figure 1: Initial DataSource
Figure 2: Transposed DataSource
The following are the screenshots of the GridGroupingControl:
Figure 3: Initial DataSource
Figure 4: Transposed DataSource
Samples:
GridDataBoundGrid:
GridGroupingControl:
C#: GGC_Transpose_CS
VB: GGC_Transpose_VB
Conclusion
I hope you enjoyed learning how to transpose the grid data in WinForms Grid Control
You can refer to WinForms Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Grid 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!