{"id":2080,"date":"2017-01-11T09:55:39","date_gmt":"2017-01-11T08:55:39","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=2080"},"modified":"2018-03-05T12:44:42","modified_gmt":"2018-03-05T11:44:42","slug":"asp-net-aspgridview-gruppieren-von-identischen-spalten-grouping-identical-gridview-rows","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=2080","title":{"rendered":"ASP.NET asp:GridView gruppieren von identischen Spalten oder nach einer bestimmten Spalte \/ grouping identical GridView Rows\/Cells or by column"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Ein vorsortiertes asp:GridView (Screenshot: Original) soll nach einer Spalte (Screenshot Algorithmus 1) oder nach identischem Inhalt (Screenshot Algorithmus 2) sortiert werden.<\/p>\n<p><a href=\"https:\/\/www.capri-soft.de\/blog\/?attachment_id=2087\" rel=\"attachment wp-att-2087\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2087\" data-permalink=\"https:\/\/www.capri-soft.de\/blog\/?attachment_id=2087\" data-orig-file=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2017\/01\/screenshot.png?fit=623%2C768&amp;ssl=1\" data-orig-size=\"623,768\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"screenshot\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2017\/01\/screenshot.png?fit=474%2C584&amp;ssl=1\" class=\"alignnone size-full wp-image-2087\" src=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2017\/01\/screenshot.png?resize=474%2C584&#038;ssl=1\" alt=\"\" width=\"474\" height=\"584\" srcset=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2017\/01\/screenshot.png?w=623&amp;ssl=1 623w, https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2017\/01\/screenshot.png?resize=243%2C300&amp;ssl=1 243w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/a><\/p>\n<h1>Pr\u00e4misse \/ Vorraussetzungen<\/h1>\n<ul>\n<li>Das GridView sollte vorher (z.B. mit ORDER BY-Klausel) vorsortiert werden um die bestm\u00f6glichen Ergebnisse zu erzielen.<\/li>\n<li>Wenn nach einer Spalte gruppiert wird, sollte diese Spalte erstrangig (also erste Erw\u00e4hnung in ORDER BY-Klausel) stattfinden.<\/li>\n<\/ul>\n<h1>Ansatz &#8211; Approach<\/h1>\n<ul>\n<li>Erschaffung eines Algorithmus, welcher \u00fcber die RowSpan-Eigenschaft Zellen miteinander verbindet.<\/li>\n<li>Vergleich ob der Text der Vorg\u00e4ngerspalte der gleiche ist\n<ul>\n<li>JA:\n<ul>\n<li>Unsichtbarschalten der aktuellen\u00a0Zeilen<\/li>\n<li>inkrement des RowSpan-Wertes der Vorg\u00e4ngerzeile<\/li>\n<li>Vorg\u00e4ngerzeile ist nun die einzig Sichtbare<\/li>\n<\/ul>\n<\/li>\n<li>Nein: Weitermachen<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>L\u00f6sung &#8211; Solution<\/h1>\n<h2>Vorgehensweise<\/h2>\n<p>1.) Auf das GridView klicken<br \/>\n2.) Im Objekt-Inspektor auf &#8222;Events\/Ereignisse&#8220; (also der Blitz) klicken<br \/>\n3.) Doppelklick auf das Ereignis &#8222;OnDataBound&#8220;<br \/>\n4.) in die erzeugte Methode kann der folgende Code kopiert werden<\/p>\n<h2>Algorithmus 1: GridView-Gruppierung nach einer Spalte<\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/****************************************\r\n* ASP:GridView Grouping Algorithmus\r\n* Quelle https:\/\/www.capri-soft.de\/blog\r\n****************************************\/\r\n\/\/ Nach dieser Spalte soll gruppiert werden\r\nint k = 1;\r\n\r\n\/\/ F\u00fcr alle Zeilen (VON UNTEN NACH OBEN)\r\nfor (int i = GridView1.Rows.Count - 1; i &gt; 0; i--)\r\n{\r\n  GridViewRow row = GridView1.Rows&#x5B;i];\r\n  GridViewRow previousRow = GridView1.Rows&#x5B;i - 1];\r\n  \/\/ F\u00fcr alle Spalten\r\n  for (int j = 0; j &amp;lt; row.Cells.Count; j++)\r\n  {\r\n    if ((row.Cells&#x5B;k].Text == previousRow.Cells&#x5B;k].Text) &amp;&amp; (row.Cells&#x5B;j].Text == previousRow.Cells&#x5B;j].Text))\r\n    {\r\n      if (previousRow.Cells&#x5B;j].RowSpan == 0)\r\n      {\r\n        if (row.Cells&#x5B;j].RowSpan == 0)\r\n        {\r\n          previousRow.Cells&#x5B;j].RowSpan += 2;\r\n        }\r\n        else\r\n        {\r\n          previousRow.Cells&#x5B;j].RowSpan =\r\n          row.Cells&#x5B;j].RowSpan + 1;\r\n        }\r\n        row.Cells&#x5B;j].Visible = false;\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<h2>Algorithmus 2: GridView-Gruppierung f\u00fcr alle identischen Zellen einer GridView-Spalte<\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/****************************************\r\n* asp:GridView Grouping Algorithmus\r\n* Quelle: https:\/\/www.capri-soft.de\/blog\r\n****************************************\/\r\n\/\/ F\u00fcr alle Zeilen (VON UNTEN NACH OBEN)\r\nfor (int i = GridView1.Rows.Count - 1; i &gt; 0; i--)\r\n{\r\n  GridViewRow row = GridView1.Rows&#x5B;i];\r\n  GridViewRow previousRow = GridView1.Rows&#x5B;i - 1];\r\n  \/\/ F\u00fcr alle Spalten\r\n  for (int j = 0; j &lt; row.Cells.Count; j++)\r\n  {\r\n    if (row.Cells&#x5B;j].Text == previousRow.Cells&#x5B;j].Text)\r\n    {\r\n      if (previousRow.Cells&#x5B;j].RowSpan == 0)\r\n      {\r\n        if (row.Cells&#x5B;j].RowSpan == 0)\r\n        {\r\n          previousRow.Cells&#x5B;j].RowSpan += 2;\r\n        }\r\n        else\r\n        {\r\n          previousRow.Cells&#x5B;j].RowSpan =\r\n          row.Cells&#x5B;j].RowSpan + 1;\r\n        }\r\n        row.Cells&#x5B;j].Visible = false;\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D2080&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem Ein vorsortiertes asp:GridView (Screenshot: Original) soll nach einer Spalte (Screenshot Algorithmus 1) oder nach identischem Inhalt (Screenshot Algorithmus 2) sortiert werden. Pr\u00e4misse \/ Vorraussetzungen Das GridView sollte vorher (z.B. mit ORDER BY-Klausel) vorsortiert werden um die bestm\u00f6glichen Ergebnisse zu erzielen. Wenn nach einer Spalte gruppiert wird, sollte diese Spalte erstrangig (also erste Erw\u00e4hnung in &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=2080\" class=\"more-link\"><span class=\"screen-reader-text\">ASP.NET asp:GridView gruppieren von identischen Spalten oder nach einer bestimmten Spalte \/ grouping identical GridView Rows\/Cells or by column<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_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","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,33],"tags":[],"class_list":["post-2080","post","type-post","status-publish","format-standard","hentry","category-net","category-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-xy","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2080"}],"version-history":[{"count":13,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2080\/revisions"}],"predecessor-version":[{"id":2630,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2080\/revisions\/2630"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}