在工作表中实现 GridDesktop 数据绑定功能
创建样本数据库
- 创建一个用于示例的数据库。我们使用Microsoft Access创建了一个具有Products表(下面是模式)的示例数据库。

- 在Products表中添加了三条虚拟记录。 Products表中的记录

创建一个示例应用程序
现在在Visual Studio中创建一个简单的桌面应用程序,并执行以下操作。
- 从工具箱中拖动"GridControl"控件并将其放在窗体上。
- 从工具箱中拖动四个按钮到窗体底部,分别设置它们的文本属性为绑定工作表,添加行,删除行和更新数据库。
添加命名空间和声明全局变量
因为这个示例使用的是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绑定
将工作表与数据集的 Products 表绑定:
- 访问所需的工作表。
- 将工作表与 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 的列标题,最好将列标题设置为数据库表中的列名。
设置工作表的列标题:
- 获取 DataSet 的 DataTable(Products) 中每个列的标题。
- 将标题分配到工作表列的标题。
在 绑定工作表 按钮的单击事件中添加以下代码片段。通过这样做,旧的列标题(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 方法。这会在底部追加一个空行,并向数据源(这里是向 DataSet 的 DataTable 中)添加新的 DataRow。开发人员可以通过多次调用 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); |
- 将上述代码添加到更新到数据库按钮。
- 运行应用程序。
- 对工作表数据执行一些操作,可能添加新行并编辑或删除现有数据。
- 然后单击更新到数据库以保存对数据库的更改。
- 检查数据库以查看表记录是否已相应更新。