In this blog we will discuss how to display the information from a database inside MindFusion.Spreadsheet for WinForms, how to validate and edit the data and how to write back any changes.
We start off by creating a new Windows Forms Application in Visual Studio, adding a WorkbookView control to the main form and adding the Northwind database (nwind.mdb) as a data source. For simplicity we only add the Categories table. After compiling the application we can add the DataSource and the CategoriesTableAdapter as components to the main form.
Loading the data
We traverse the rows in the data source and populate the spreadsheet by assigning the data to the Data property of the respective worksheet cells. To prevent certain columns from being edited (for example auto-increment keys and image fields), we mark these columns by setting their Tag property. All columns and rows beyond those that actually display data are hidden by setting their IsHidden property to true. The column titles are set to the names of the corresponding database columns. Finally, the columns are resized to fit their contents through the ResizeColumnsToFit method of the view. The complete code of the data loading can be found in the LoadData method.
Note, that during the data loading, the loading flag is set to true. This is done to prevent some unnecessary operations in the CellChanging and CellChanged event handlers.
Setting up the view
To resemble a data grid, we need to disable certain functions in the view. More specifically, we need to hide the tabs, the formula bar, the auto-fill handle and the hidden header indicator.
workbookView1.ShowHiddenHeaderIndicators = false; workbookView1.AllowAutoFill = false; workbookView1.ShowTabs = false; workbookView1.ShowFormulaBar = false;
We want to prevent the users from changing the values of certain cells – for example, the cells in the auto-increment column and the cells representing pictures. To do this, we will handle the InplaceEditStarting event of the WorkbookView and the WorksheetCellChanging event of the Workbook. In the event handlers we check the Tag value of the related column. If the column is marked as read-only, we set the Cancel property of the event argument to true, to prevent the edit or change.
Adding new rows
When we populated the data from the dataset, we left an empty row at the bottom of the spreadsheet. The intent is that this empty row should be used to add new rows to the table. When the user edits a cell of the empty row, the row key is automatically calculated and a new empty row is added at the bottom of the spreadsheet. To implement this functionality, we handle the WorksheetCellChanged event of the Workbook. In the event handler, we inspect the row of the edited cell. If this is the last visible row in the spreadsheet, we calculate a key for this row and reveal another row at the bottom of the spreadsheet by setting its IsHidden property to false.
Saving changes to the database
Changes are immediately reflected back to the database from within the WorksheetCellChanged event handler. New rows are added via the Rows.Add method of the Categories data table. Existing rows are updated directly through the respective DataRow object. The changes are effectively performed by calling the Update method of the table adapter.
The following image shows the running sample:
The source code is available for download from here:
The trial version of MindFusion.Spreadsheet for WinForms can be downloaded from here:
About MindFusion.Spreadsheet for WinForms: A powerful .net spreadsheet component with great capabilities for editing, styling and formatting large amounts of data.