Category / Section
How to get all the data in a GridControl in an array?
2 mins read
Problem:
By using an indexer, you can retrieve the grid[row, col].CellValue that triggers the events like QueryCellInfo which slows things down.
Solution:
To avoid triggering these events, you can access the GridData directly and retrieve the data stored in the Grid.
C#
//access data by using the GridData object.... GridData gridData = this.gridControl1.Data; int numRows = gridData.RowCount; int numCols = gridData.ColCount; Console.WriteLine("{0} rows by {1} cols", numRows, numCols); for (int i = 1; i <= numRows; ++i) { for (int j = 1; j <= numCols; ++j) { int arrayCount = 0; if (gridData[i, j] != null) { // creates the style object to get the info for particular cell GridStyleInfo style = new GridStyleInfo(gridData[i, j]); // adds the details to the array strArray[arrayCount] = style.Text; // writes the output as the values stored in the grid. Console.Write(strArray[arrayCount] + " "); arrayCount++; } else Console.Write("empty"); } Console.WriteLine(""); }
VB
'access data by using the GridData object.... Dim gridData As GridData = Me.gridControl1.Data Dim numRows As Integer = gridData.RowCount Dim numCols As Integer = gridData.ColCount Console.WriteLine("{0} rows by {1} cols", numRows, numCols) For i As Integer = 1 To numRows For j As Integer = 1 To numCols Dim arrayCount As Integer = 0 If gridData(i, j) IsNot Nothing Then ' creates the style object to get the info for particular cell Dim style As New GridStyleInfo(gridData(i, j)) 'Adds the details to the array strArray(arrayCount) = style.Text 'Writes the output as the values stored in the grid. Console.Write(strArray(arrayCount) & " ") arrayCount += 1 Else Console.Write("empty") End If Next j Console.WriteLine("") Next i
Figure 1: Grid screen shot
Figure 2: Output screen shot
Sample Link: