{"id":4119,"date":"2022-01-13T16:30:18","date_gmt":"2022-01-13T16:30:18","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4119"},"modified":"2022-01-13T16:30:19","modified_gmt":"2022-01-13T16:30:19","slug":"9d-wxwidgets-wxgrid","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/13\/9d-wxwidgets-wxgrid\/","title":{"rendered":"9d. wxWidgets &#8211; wxGrid"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>wxGrid sets up a grid with rows and columns, much like a spreadsheet and then add text to each of the cells. That is the basic wxGrid implementation. You can subclass it to create more sophisticated tabular data display of complex objects.<\/p>\n\n\n\n<p>The sample code below creates a wxGrid with custom colors, content and cell formatting. The first column is read-only containing employee codes. The second column allows entry of any string for employee name. The third column only accepts float or double values for salary amount.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>grid.h<\/strong><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\n\nclass Grid: public wxFrame {\n\tpublic:\n\t\tGrid(const wxString &amp;title);\t\n\n\tprivate:\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">grid.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;grid.h&quot;\n#include &lt;wx\/grid.h&gt;\n\nGrid::Grid(const wxString &amp;title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(700,550)) {\n\twxPanel *panel = new wxPanel(this, -1);\n\twxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n\tpanel-&gt;SetSizer(vbox);\n\n\twxGrid *grid = new wxGrid(panel, -1);\n\tgrid-&gt;CreateGrid(40,3);\n\tgrid-&gt;SetColSize(0,100);\n\tgrid-&gt;SetColSize(1,400);\n\tgrid-&gt;SetColSize(2,100);\n\n\tgrid-&gt;SetColFormatFloat(2,6,2);\n\n\twxString empCode = wxT(&quot;Emp#&quot;);\n\tfor(int i = 0; i &lt; 40; i++) {\n\t   empCode = wxT(&quot;Emp#&quot;);\n\t   grid-&gt;SetCellValue(i, 0, empCode.Append(wxString::Format(wxT(&quot;%d&quot;), (i+1))));\n\t   grid-&gt;SetReadOnly(i,0);\n   \t   grid-&gt;SetCellTextColour(i, 0, *wxGREEN);\n\t   grid-&gt;SetCellBackgroundColour(i, 0, wxColor(100,100,100));\n\t   grid-&gt;SetCellBackgroundColour(i, 1, wxColor(200,80,45));\n\t   grid-&gt;SetCellBackgroundColour(i, 2, wxColor(45,19,177));\n\t}\n\n\tvbox-&gt;Add(grid, 1, wxEXPAND);\n\n\tCenter();\n\n\n}\n\n<\/pre><\/div>\n\n\n<p>main.h<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;grid.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tGrid *app = new Grid(wxT(&quot;Grid&quot;));\n\tapp-&gt;Show(true);\n\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"550\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-13-21-58-52.png\" alt=\"\" class=\"wp-image-4126\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-13-21-58-52.png 700w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-13-21-58-52-620x487.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-13-21-58-52-300x236.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW wxGrid sets up a grid with rows and columns, much like a spreadsheet and then add text to each of the cells. That is <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/13\/9d-wxwidgets-wxgrid\/\" title=\"9d. wxWidgets &#8211; wxGrid\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4119"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4119\/revisions"}],"predecessor-version":[{"id":4128,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4119\/revisions\/4128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}