将 DataView 导入到 GridWeb
Contents
[
Hide
]
随着 Microsoft .NET Framework 的发布,引入了一种新的存储数据的方式。现在能够使用 DataSet、DataTable 和 DataView 对象以离线模式存储数据。这些对象非常方便,可作为数据存储库。使用 Aspose.Cells.GridWeb,可以从 DataTable 或 DataView 对象中导入数据到工作表。Aspose.Cells.GridWeb 仅支持从 DataView 中导入数据,但 DataTable 对象也可以间接使用。让我们详细讨论此功能。
从 DataView 导入数据
使用 GridWeb 控件中的 GridWorsheetCollection 的 ImportDataView 方法从 DataView 对象中导入数据。将要导入数据的 DataView 对象传递给 ImportDataView 方法。在导入期间可以指定列标题和数据样式。
从 DataView 对象导入数据时,将创建一个新的工作表来保存导入的数据。该工作表与 DataTable 同名。
输出:从 DataView 导入的数据到新工作表
调整列宽以显示它们包含的所有数据。在从 DataView 导入数据时,列宽不会自动调整。用户需要自行调整它们。要通过编程方式调整列宽,请参阅调整行和列大小。
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); |
ImportDataView 方法的重载版本允许开发人员指定保存导入数据的工作表的名称,以及从 DataView 对象中导入的特定行数和列数。