Import DataView to GridWeb
Contents
[
Hide
]
With the release of the Microsoft .NET Framework, a new way of storing data was introduced. Now DataSet, DataTable and DataView objects that store data in offline mode. These objects are very handy as data repositories. Using Aspose.Cells.GridWeb, it’s possible to import data from either DataTable or DataView objects into worksheets. Aspose.Cells.GridWeb only supports importing data from a DataView. object but a DataTable object can also be used indirectly. Let’s discuss this feature in detail.
Importing Data from DataView
Import data from a DataView object using the GridWorsheetCollection’s ImportDataView method in the GridWeb control. Pass the DataView object that you want to import data from to the ImportDataView method. It’s possible to specify column header and data styles during import.
When data is imported from a DataView object, a new worksheet is created to hold the imported data. The worksheet is named the same as the DataTable.
Output: Data imported from a DataView into a new worksheet
The widths of the columns are adjusted to show all the data they contain. When the data is imported from DataView, column widths are not adjusted automatically. Users need to adjust them by themselves. To adjust the column widths programmatically, refer to Resize Rows and Columns.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Connect database | |
System.Data.OleDb.OleDbConnection oleDbConnection1 = new OleDbConnection(); | |
System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1 = new OleDbDataAdapter(); | |
System.Data.OleDb.OleDbCommand oleDbSelectCommand1 = new OleDbCommand(); | |
string path = (this.Master as Site).GetDataDir(); | |
oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "\\Worksheets\\Database\\Northwind.mdb"; | |
oleDbSelectCommand1.Connection = oleDbConnection1; | |
oleDbDataAdapter1.SelectCommand = oleDbSelectCommand1; | |
DataTable dataTable1 = new DataTable(); | |
dataTable1.Reset(); | |
// Queries database. | |
try | |
{ | |
oleDbSelectCommand1.CommandText = "SELECT CategoryID, CategoryName, Description FROM Categories"; | |
oleDbDataAdapter1.Fill(dataTable1); | |
} | |
catch | |
{ | |
} | |
finally | |
{ | |
oleDbConnection1.Close(); | |
} | |
// Imports data from dataview object. | |
dataTable1.TableName = "Categories"; | |
GridWeb1.WorkSheets.Clear(); | |
GridWeb1.WorkSheets.ImportDataView(dataTable1.DefaultView, null, null); | |
// Imports data from dataview object with sheet name and position specified. | |
GridWeb1.WorkSheets.ImportDataView(dataTable1.DefaultView, null, null, "SpecifiedName&Position", 2, 1); |
An overloaded version of the ImportDataView method allows developers to specify the name of the sheet that holds the imported data and a specific number of rows and columns to import from the DataView object.