在工作表中实现 GridDesktop 数据绑定功能

创建示例数据库

  1. 创建一个示例数据库以用于示例。我们使用 Microsoft Access 创建了一个带有 Products 表(架构如下)的示例数据库。

待办事项:图片_替代_文本

  1. Products 表中添加了三个虚拟记录。 产品表中的记录

待办事项:图片_替代_文本

创建示例应用程序

现在在 Visual Studio 中创建一个简单的桌面应用程序并执行以下操作。

  1. 将“GridControl”控件从工具箱中拖放到窗体上。
  2. 从窗体底部的工具箱中拖放四个按钮并将它们的文本属性设置为绑定工作表, 添加行, 删除行更新数据库分别。

添加命名空间和声明全局变量

因为此示例使用 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;

您现在可以使用打包在此名称空间下的类。

  1. 声明全局变量。
// 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 对象中。

  1. 使用 OleDbDataAdapter 对象连接我们的示例数据库,并使用从数据库中的 Products 表中获取的数据填充数据集,如下面的代码所示。
// 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 的 Products 表绑定:

  1. 访问所需的工作表。
  2. 将工作表与 DataSet 的 Products 表绑定。

将以下代码添加到绑定工作表按钮的点击事件。

// 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。最好将列标题设置为数据库表中的列名。

设置工作表的列标题:

  1. 获取 DataSet 中 DataTable (Products) 每一列的标题。
  2. 将标题分配给工作表列的标题。

附上写在绑定工作表按钮的点击事件,代码如下。通过这样做,旧的列标题(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 支持更改列宽。

将以下代码附加到绑定工作表按钮。列宽将根据新设置进行自定义。

// 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 还支持对列应用自定义样式。下面的代码,附加到绑定工作表按钮,自定义列样式以使其更美观。

// 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);
}

现在运行应用程序并单击绑定工作表按钮。

添加行

要向工作表添加新行,请使用 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);
  1. 将上面的代码添加到更新数据库按钮。
  2. 运行应用程序。
  3. 对工作表数据执行一些操作,可能会添加新行以及编辑或删除现有数据。
  4. 然后点击更新数据库将更改保存到数据库。
  5. 检查数据库,查看表记录是否已相应更新。