Articles in this section
Category / Section

How to connect to SQL Server with EJ2 PivotGrid

1 min read

The following steps explain how to connect and retrieve the data from SQL Database and then bind the data with pivot grid.

Step 1:

The following code snippet illustrates retrieving the data from SQL database.

C#

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = new SqlCommand(query, connection);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
for (var i = 0; i < dataTable.Rows.Count; i++)
{
    for (var j = 0; j < dataTable.Rows[i].ItemArray.Length; j++)
    {
        dataTable.Rows[i].ItemArray[j] = dataTable.Rows[i].ItemArray[j].ToString().Trim();
    }
}
ViewBag.data = dataTable;

 

Step 2:

After retrieving the data from database, bind it to the pivot grid using the <e-dataSource data> property.

CSHTML

<ejs-pivotview id="pivotview">
    <e-datasource data="@ViewBag.data"></e-datasource>       
</ejs-pivotview>

 

 

The following screenshot displays a simple pivot grid bounded to data from SQL Database. Here, ‘Country’ field in row, ‘Product’ field in column, and ‘Amount’ field in value are bounded. Check complete code from the sample, which is attached in the following link.

 

SQL Database Pivot Grid screenshot

 

Sample link: SQL data binding

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied