{"id":787,"date":"2014-05-07T12:31:58","date_gmt":"2014-05-07T12:31:58","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=787"},"modified":"2021-01-14T11:28:42","modified_gmt":"2021-01-14T11:28:42","slug":"winforms-spreadsheet-apply-conditional-formatting","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/winforms-spreadsheet-apply-conditional-formatting\/","title":{"rendered":"WinForms.Spreadsheet: Apply Conditional Formatting"},"content":{"rendered":"<p>In this blog we will discuss how to apply conditional formatting to a range of cells in MindFusion.Spreadsheet for WinForms. Conditional formatting is a feature that allows you to apply different formatting options, such as background color, borders, or font to cells that meet certain criteria and have this formatting automatically change depending on the value or formula of the cell.<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>We start off by creating a new Windows Forms Application in Visual Studio and adding a WorkbookView control to the main form. The WorkbookView displays a Workbook with a single worksheet.<\/p>\n<p><strong>Initializing the data<\/strong><\/p>\n<p>The goal of the application is to display an array of temperature values, colored differently based on a standard temperature scale. The data of the application is hard-coded in several arrays, which are used to initialize the worksheet:<\/p>\n<pre>var names = new CultureInfo(\"en-US\").DateTimeFormat.AbbreviatedMonthNames;\nvar avgHigh = new int[] { 4, 7, 12, 17, 21, 25, 28, 28, 23, 18, 10, 4 };\nvar avgLow = new int[] { -5, -4, 0, 5, 9, 13, 14, 14, 10, 6, 0, -4 };\nvar recordHigh = new int[] { 16, 17, 24, 28, 29, 32, 37, 34, 34, 30, 24, 17 };\nvar recordLow = new int[] { -21, -19, -15, -5, -1, 4, 7, 8, -2, -2, -7, -16 };<\/pre>\n<p><strong>Applying the conditional formatting<\/strong><\/p>\n<p>In MindFusion.Spreadsheet conditional formatting is applied to a range of cells through its style. This is done by calling the Style.ConditionalFormats.Add method to create IConditionalFormat objects and then setting the properties of the newly created object to specify the condition criteria and the style to apply when the criteria are met. In this application we use a simple algorithm to distribute temperature values linearly along a blue-yellow-red color scale. When a cell value falls within a specific temperature interval, the conditional formatting applies a background color to this cell to visually indicate the value.<\/p>\n<p>The following image shows the running sample:<\/p>\n<p><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformats.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-791\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformats-300x214.png\" alt=\"spreadsheet-conditionalformats\" width=\"300\" height=\"214\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformats-300x214.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformats.png 739w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Conditional formatting can also be created and modified through the built-in ConditionalFormatForm form, contained in the <em>MindFusion.Spreadsheet.WinForms.StandardForms.dll<\/em> assembly. The image below displays the form for the temperature cell range.<\/p>\n<p><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformatform.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-792\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformatform-300x300.png\" alt=\"spreadsheet-conditionalformatform\" width=\"300\" height=\"300\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformatform-300x300.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformatform-150x150.png 150w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2014\/05\/spreadsheet-conditionalformatform.png 549w\" 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\/SpreadsheetConditionalFormats.zip\">https:\/\/mindfusion.dev\/_samples\/SpreadsheetConditionalFormats.zip<\/a><\/p>\n<p>The trial version of MindFusion.Spreadsheet for WinForms can be downloaded from here:<\/p>\n<p><a title=\"Download MindFusion.Spreadsheet for WinForms Trial Version\" 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 apply conditional formatting to a range of cells in MindFusion.Spreadsheet for WinForms. Conditional formatting is a feature that allows you to apply different formatting options, such as background color, borders, or font &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/winforms-spreadsheet-apply-conditional-formatting\/\">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":[210,209,165,164,192],"class_list":["post-787","post","type-post","status-publish","format-standard","hentry","category-sample-code","category-spreadsheet","tag-cells","tag-conditional-formatting","tag-excel","tag-spreadsheet-2","tag-worksheet"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-cH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/787","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=787"}],"version-history":[{"count":4,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions"}],"predecessor-version":[{"id":2510,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions\/2510"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}