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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get all the data in a GridControl in an array?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: May 28, 2019
Tags: data

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

 

Showing grid data in Grid

Figure 1: Grid screen shot

 

Showing output grid data

Figure 2: Output screen shot

 

Sample Link:

C#: GetGridDataAsArray

VB: GetGridDataAsArray

 

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.

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