{"id":848,"date":"2011-08-17T20:20:56","date_gmt":"2011-08-18T01:20:56","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=848"},"modified":"2021-03-10T20:43:17","modified_gmt":"2021-03-11T01:43:17","slug":"powerbuilder-grid-datawindow-object-with-variable-number-of-columns","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-grid-datawindow-object-with-variable-number-of-columns\/","title":{"rendered":"PowerBuilder &#8211; Grid datawindow object with variable number of columns"},"content":{"rendered":"<p>Here is a way to build a grid datawindow which contains columns corresponding to an unknown number of data elements. You could use this approach in creating a project schedule, inventory location system, baseball box score, or any number of other examples. My example assumes the minimum number of columns to be four.<\/p>\n<p>This create grid event returns a string.<\/p>\n<pre name=\"code\" class=\"VB\">long\tll_columns, ll_count, ll_pos\nstring ls_errors, ls_sql, ls_dw_presentation, ls_dw_syntax, ls_find, ls_name, ls_mod_string\n\/\/ base presentation information\nls_dw_presentation =    &quot;style( type=Grid                          &amp;\n            Horizontal_spread = 25 )            &amp;\n     datawindow( units=1                       &amp;\n             Color= 16777215)              &amp;\n     column( Font.Face=&#039;Arial&#039;         &amp;\n         Font.Height=-9                    &amp;\n         Font.Weight=400)                  &amp;\n         text(   Font.Face=&#039;Arial&#039; &amp;\n         Font.Height=-9                    &amp;\n         Font.Weight=400                   &amp;\n             Border=6)&quot;\n\n\/\/ set initial grid to 4 columns (plus time_of_day and last_col columns)\nls_sql = &quot;SELECT space(10) as time_of_day, space(40) as col1, space(40) as col2, space(40) as col3, space(40) as col4,&quot;\n\nll_count = 5 \/\/ next column to add would be 5\n\n\/\/ this is the variable number of data elements\nll_columns = ids_trains.rowcount()\n\n\/\/ add any additional columns past 4\nDO WHILE ll_count &lt;= ll_columns\n\tls_sql += &quot; space(40) as col&quot; + string(ll_count) + &quot;,&quot;\n\tll_count++\nLOOP\n\nls_sql += &quot; &#039;&#039; as last_col&quot;\nls_sql += &quot; FROM dbo.TRAINS WHERE 1 = 2&quot;\n\/\/ build the datawindow syntax from the presentation and sql statement\nls_dw_syntax = SQLCA.Syntaxfromsql( ls_sql, ls_dw_presentation, ls_errors)\nIF Len(ls_errors) = 0 THEN\n\t\/\/ widen the header band a bit\n\tls_dw_syntax = Replace(ls_dw_syntax, Pos(ls_dw_syntax,&quot;header(height=19)&quot;), Len(&quot;header(height=19)&quot;), &quot;header(height=23)&quot;)\n\t\/\/ create dwo and assign to grid\n\tdw_grid.create(ls_dw_syntax,ls_errors)\nEND IF\n\/\/ appearance modifies\ndw_grid.modify(&quot;last_col.visible=FALSE&quot;)\n\nFOR ll_count = 1 TO Long(dw_grid.Object.DataWindow.Column.Count)\n\tIF dw_grid.Describe(&quot;#&quot; + String(ll_count) + &quot;.Visible&quot;) = &quot;1&quot; Then\n\t\tls_name = dw_grid.Describe(&quot;#&quot; + String(ll_count) + &quot;.Name&quot;) \n\t\tls_mod_string = ls_name + &quot;_t.background.mode=0&quot; \/\/ must set to opaque or color does not show\n\t\tls_errors = dw_grid.modify(ls_mod_string)\n\t\t\/\/ set the column heading color\n\t\tls_mod_string = ls_name + &quot;_t.background.color=12632256&quot;\n\t\tls_errors = dw_grid.modify(ls_mod_string)\n\t\tIF POS(ls_name ,&#039;time_of_day&#039;) = 0 THEN\n\t\t\tls_mod_string = ls_name + &quot;_t.Alignment=0&quot; \/\/left\n\t\t\tls_errors = dw_grid.modify(ls_mod_string)\n\t\tEND IF\n\t\tls_mod_string = ls_name + &quot;_t.Height=18&quot;\n\t\tls_errors = dw_grid.modify(ls_mod_string)\n\t\tls_mod_string = ls_name + &quot;_t.tooltip.enabled=1&quot;\n\t\tls_errors = dw_grid.modify(ls_mod_string)\n\tEND IF\nNEXT\nRETURN ls_errors<\/pre>\n<p>Now I know you can do several modifies with a single statement but it is much easier to debug if you do a single attribute at a time.<\/p>\n<p>Once you have the grid set up you can call subsequent methods to populate it (and make columns invisible if needed), resize the width of the columns, etc.<\/p>\n<p><a href=\"http:\/\/anvil-of-time.com\/wordpress\/wp-content\/uploads\/2011\/08\/schedule.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-849 size-full\" title=\"schedule\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/08\/schedule.png\" alt=\"\" width=\"466\" height=\"221\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/08\/schedule.png 466w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/08\/schedule-300x142.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2011\/08\/schedule-150x71.png 150w\" sizes=\"(max-width: 466px) 100vw, 466px\" \/><\/a><br \/>\nExample grid for a time schedule with fifteen minute increments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a way to build a grid datawindow which contains columns corresponding to an unknown number of data elements. You could use this approach in creating a project schedule, inventory location system, baseball box score, or any number of other examples. My example assumes the minimum number of columns to be four. This create&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[37,21,12,16],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/848"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=848"}],"version-history":[{"count":6,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/848\/revisions"}],"predecessor-version":[{"id":1972,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/848\/revisions\/1972"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}