DataView を GridWeb にインポートする
Contents
[
Hide
]
Microsoft .NET フレームワークのリリースにより、データを格納する新しい方法が導入されました。現在、オフライン モードでデータを保存する 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 オブジェクトからインポートする特定の数の行と列を指定できます。