ワークシートでのGridDesktopデータバインディング機能の実装
サンプル データベースの作成
- 例と一緒に使用するためのサンプル データベースを作成します。Microsoft Access を使用して、Products テーブルを含むサンプル データベースを作成しました(以下にスキーマを示します)。
- Productsテーブルにダミーレコード3件を追加します。 Productsテーブルのレコード
サンプルアプリケーションの作成
Visual Studioでシンプルなデスクトップアプリケーションを作成し、以下の手順を実行します。
- ツールボックスから"GridControl"コントロールをドラッグしてフォーム上に配置します。
- ツールボックスからボタンを4つフォームの下部にドロップし、それぞれのテキストプロパティをBind Worksheet、Add Row、Delete Row、Update to Databaseに設定します。
名前空間の追加とグローバル変数の宣言
この例では、Microsoft Accessデータベースを使用しているため、コードの先頭にSystem.Data.OleDb名前空間を追加します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Adding namespace to the top of code | |
using System.Data.OleDb; |
この名前空間で提供されているクラスを使用できます。
- グローバル変数を宣言します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Declaring global variable | |
OleDbDataAdapter adapter; | |
OleDbCommandBuilder cb; | |
DataSet ds; |
データベースからデータを取得してDataSetを埋める
サンプルデータベースに接続して、DataSetオブジェクトにデータを取得して埋めます。
- OleDbDataAdapterオブジェクトを使用してサンプルデータベースに接続し、データベースのProductsテーブルから取得したデータでDataSetを埋めます。以下のコードに示すように。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
private void DataBindingFeatures_Load(object sender, EventArgs e) | |
{ | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating Select query to fetch data from database | |
string query = "SELECT * FROM Products ORDER BY ProductID"; | |
// Creating connection string to connect with database | |
string conStr = @"Provider=microsoft.jet.oledb.4.0;Data Source=" + dataDir + "dbDatabase.mdb"; | |
// Creating OleDbDataAdapter object that will be responsible to open/close connections with database, fetch data and fill DataSet with data | |
adapter = new OleDbDataAdapter(query, conStr); | |
// Setting MissingSchemaAction to AddWithKey for getting necesssary primary key information of the tables | |
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; | |
/* | |
* Creating OleDbCommandBuilder object to create insert/delete SQL commmands | |
* automatically that are used by OleDbDatAdapter object for updating | |
* changes to the database | |
*/ | |
cb = new OleDbCommandBuilder(adapter); | |
// Creating DataSet object | |
ds = new DataSet(); | |
// Filling DataSet with data fetched by OleDbDataAdapter object | |
adapter.Fill(ds, "Products"); | |
} |
DataSetとWorksheetのバインディング
WorksheetをDataSetのProductsテーブルにバインドします:
- 任意のワークシートにアクセスします。
- ワークシートをDataSetのProductsテーブルにバインドします。
Bind Worksheetボタンのクリックイベントに以下のコードを追加します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Binding the Worksheet to Products table by calling its DataBind method | |
sheet.DataBind(ds.Tables["Products"], ""); |
ワークシートの列見出しの設定
バインドされたワークシートはデータを正常に読み込みますが、列見出しはデフォルトでA、B、Cとラベル付けされています。データベーステーブルの列名に列見出しを設定するのがより良いでしょう。
ワークシートの列見出しを設定するには:
- DataSetのDataTable(Products)の各列のキャプションを取得します。
- キャプションをワークシートの列見出しに割り当てます。
Bind Worksheetボタンのクリックイベントに以下のコードスニペットを追加します。これにより、古い列見出し(A、B、C)はProductID、ProductName、ProductPriceで置き換えられます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Iterating through all columns of the Products table in DataSet | |
for (int i = 0; i < ds.Tables["Products"].Columns.Count; i++) | |
{ | |
// Setting the column header of each column to the column caption of Products table | |
sheet.Columns[i].Header = ds.Tables["Products"].Columns[i].Caption; | |
} |
列の幅とスタイルのカスタマイズ
さらにワークシートの外観を改善するために、列の幅とスタイルを設定することができます。たとえば、列見出しや列内の値がセルに収まらない長い文字数で構成されている場合があります。このような問題を解決するために、Aspose.Cells.GridDesktopは列の幅を変更することをサポートしています。
Bind Worksheetボタンに以下のコードを追加します。列の幅は新しい設定に従ってカスタマイズされます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Customizing the widths of columns of the worksheet | |
sheet.Columns[0].Width = 70; | |
sheet.Columns[1].Width = 120; | |
sheet.Columns[2].Width = 80; |
Aspose.Cells.GridDesktopは列にカスタムスタイルを適用することもサポートしています。Bind Worksheetボタンに追加された以下のコードは、列のスタイルをカスタマイズして見栄えを向上させます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Iterating through each column of the worksheet | |
for (int i = 0; i < sheet.ColumnsCount; i++) | |
{ | |
// Getting the style object of each column | |
Style style = sheet.Columns[i].GetStyle(); | |
// Setting the color of each column to Yellow | |
style.Color = Color.Yellow; | |
// Setting the Horizontal Alignment of each column to Centered | |
style.HAlignment = HorizontalAlignmentType.Centred; | |
// Setting the style of column to the updated one | |
sheet.Columns[i].SetStyle(style); | |
} |
アプリケーションを実行し、Bind Worksheetボタンをクリックします。
行の追加
ワークシートに新しい行を追加するには、WorksheetクラスのAddRowメソッドを使用します。これにより、空の行が一番下に追加され、新しいDataRowがデータソースに追加されます(ここでは、新しいDataRowがDataSetのDataTableに追加されます)。開発者は、AddRowメソッドを繰り返し呼び出すことで、必要なだけ多くの行を追加できます。行が追加された後、ユーザーはその行に値を入力することができます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Adding new row to the worksheet | |
gridDesktop1.GetActiveWorksheet().AddRow(); |
行の削除
Aspose.Cells.GridDesktopでは、WorksheetクラスのRemoveRowメソッドを呼び出すことで行を削除することもサポートされています。Aspose.Cells.GridDesktopを使用して行を削除するには、削除する行のインデックスが必要です。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Getting the index of the focused row | |
int focusedRowIndex = gridDesktop1.GetActiveWorksheet().GetFocusedCell().Row; | |
// Removing the focused row fro the worksheet | |
gridDesktop1.GetActiveWorksheet().RemoveRow(focusedRowIndex); |
上記のコードを行の削除ボタンに追加し、アプリケーションを実行します。行が削除される前にいくつかのレコードが表示されます。行を選択し、行の削除ボタンをクリックすると、選択した行が削除されます。
データベースへの変更の保存
最後に、ユーザーがワークシートに行った変更をデータベースに保存するには、OleDbDataAdapterオブジェクトのUpdateメソッドを使用します。Updateメソッドは、ワークシートのデータソース(DataSet、DataTableなど)を取り、それらの変更をデータベースに反映させます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Updating the database according to worksheet data source | |
adapter.Update((DataTable)sheet.DataSource); |
- 上記のコードをデータベースに更新ボタンに追加します。
- アプリケーションを実行します。
- ワークシートのデータに操作を行い、新しい行を追加したり、既存のデータを編集したり削除することがあります。
- 次にデータベースに更新をクリックして、変更をデータベースに保存します。
- テーブルのレコードが適切に更新されたかどうかを確認します。