{"id":689,"date":"2014-02-20T11:18:32","date_gmt":"2014-02-20T11:18:32","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=689"},"modified":"2021-01-11T16:26:13","modified_gmt":"2021-01-11T16:26:13","slug":"winforms-spreadsheet-databinding","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/winforms-spreadsheet-databinding\/","title":{"rendered":"WinForms Spreadsheet Databinding"},"content":{"rendered":"<p>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.<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>We start off by creating a new Windows Forms Application in Visual Studio, adding a <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?T_MindFusion_Spreadsheet_WorkbookView.htm\">WorkbookView<\/a> 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.<\/p>\n<p><strong>Loading the data<\/strong><\/p>\n<p>We traverse the rows in the data source and populate the spreadsheet by assigning the data to the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?P_MindFusion_Spreadsheet_Cell_Data_0.htm\">Data<\/a> 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 <strong>IsHidden<\/strong> 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 <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?M_MindFusion_Spreadsheet_WorkbookView_ResizeColumnsToFit_2_Int32_Int32.htm\">ResizeColumnsToFit<\/a> method of the view. The complete code of the data loading can be found in the LoadData method.<\/p>\n<p>Note, that during the data loading, the <i>loading<\/i> flag is set to true. This is done to prevent some unnecessary operations in the CellChanging and CellChanged event handlers.<\/p>\n<p><strong>Setting up the view<\/strong><\/p>\n<p>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.<\/p>\n<pre>workbookView1.ShowHiddenHeaderIndicators = false;\nworkbookView1.AllowAutoFill = false;\nworkbookView1.ShowTabs = false;\nworkbookView1.ShowFormulaBar = false;<\/pre>\n<p><strong>Performing validation<\/strong><\/p>\n<p>We want to prevent the users from changing the values of certain cells &#8211; for example, the cells in the auto-increment column and the cells representing pictures. To do this, we will handle the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?E_MindFusion_Spreadsheet_WorkbookView_InplaceEditStarting.htm\">InplaceEditStarting<\/a> event of the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?T_MindFusion_Spreadsheet_WorkbookView.htm\">WorkbookView<\/a> and the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?E_MindFusion_Spreadsheet_Workbook_WorksheetCellChanging.htm\">WorksheetCellChanging<\/a> event of the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?T_MindFusion_Spreadsheet_Workbook.htm\">Workbook<\/a>. In the event handlers we check the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?P_MindFusion_Spreadsheet_Column_Tag_0.htm\">Tag<\/a> value of the related column. If the column is marked as read-only, we set the <strong>Cancel<\/strong> property of the event argument to true, to prevent the edit or change.<\/p>\n<p><strong>Adding new rows<\/strong><\/p>\n<p>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 <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?E_MindFusion_Spreadsheet_Workbook_WorksheetCellChanged.htm\">WorksheetCellChanged<\/a> event of the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?T_MindFusion_Spreadsheet_Workbook.htm\">Workbook<\/a>. 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 <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?P_MindFusion_Spreadsheet_Row_IsHidden_0.htm\">IsHidden<\/a> property to false.<\/p>\n<p><strong>Saving changes to the database<\/strong><\/p>\n<p>Changes are immediately reflected back to the database from within the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/spreadsheetwinforms\/index.htm?E_MindFusion_Spreadsheet_Workbook_WorksheetCellChanged.htm\">WorksheetCellChanged<\/a> 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.<\/p>\n<p>The following image shows the running sample:<\/p>\n<p><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/02\/spreadsheet-databinding.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-694\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/02\/spreadsheet-databinding-300x214.png\" alt=\"spreadsheet-databinding\" width=\"300\" height=\"214\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/02\/spreadsheet-databinding-300x214.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/02\/spreadsheet-databinding.png 739w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The source code is available for download from here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/_samples\/SpreadsheetDatabase.zip\">https:\/\/mindfusion.dev\/_samples\/SpreadsheetDatabase.zip<\/a><\/p>\n<p>The trial version of MindFusion.Spreadsheet for WinForms can be downloaded from here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/SpreadsheetTrial.zip\">Download MindFusion.Spreadsheet for WinForms Trial Version<\/a><\/p>\n<p><em>About MindFusion.Spreadsheet for WinForms<\/em>: A powerful .net spreadsheet component with great capabilities for editing, styling and formatting large amounts of data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. Introduction We start off by creating a new &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/winforms-spreadsheet-databinding\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[74,163],"tags":[193,165,164,192],"class_list":["post-689","post","type-post","status-publish","format-standard","hentry","category-sample-code","category-spreadsheet","tag-database","tag-excel","tag-spreadsheet-2","tag-worksheet"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-b7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":9,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"predecessor-version":[{"id":2484,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/689\/revisions\/2484"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}