{"id":549,"date":"2011-01-06T20:38:53","date_gmt":"2011-01-06T20:38:53","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=549"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"creating-manual-data-grid-from-arrays","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/datagridview\/creating-manual-data-grid-from-arrays","title":{"rendered":"Creating Data Grid From Arrays"},"content":{"rendered":"<p>\nGood resources:<br \/>\n<a href=\"http:\/\/www.devx.com\/dotnet\/Article\/33748\">http:\/\/www.devx.com\/dotnet\/Article\/33748<\/a>\n<\/p>\n<h4>\nFirst Create A Class To Be Able To Add Data<br \/>\n<\/h4>\n<p>\nCreate a class which has a variable for each column the datagrid will have &#8211; the class will be created for each data grid row. You can implement how these properties get set however you want, but a good way is to write them as part of the constructor.\n<\/p>\n<pre>\r\n<code>\r\n\tref class GridEntry\r\n\t{\r\n\tpublic:\r\n\t\tproperty String ^GroupName;\t\/\/Order here important - this is right most column\r\n\t\tproperty bool Overloaded;\r\n\t\tproperty bool Ok;\r\n\t\tproperty bool Underloaded;\r\n\r\n\t\tpublic: GridEntry::GridEntry(String ^NewGroupName, bool NewOverloaded, bool NewOverloadWarning, bool NewOk, bool NewUnderloadWarning, bool NewUnderloaded)\r\n\t\t{\r\n\t\t\tGroupName = NewGroupName;\r\n\t\t\tOverloaded = NewOverloaded;\r\n\t\t\tOk = NewOk;\r\n\t\t\tUnderloaded = NewUnderloaded;\r\n\t\t}\r\n\t};\r\n<\/code><\/pre>\n<p>\n<span style=\"color: rgb(51, 51, 51); font-size: 10px;\">To add the class to a form see <a href=\"https:\/\/ibex.tech\/visualcpp\/programming\/visual-cpp-cli-dot-net\/classes-objects-struct\/adding-new-class-to-a-form\">here<\/a><\/span>\n<\/p>\n<h4>\nAdd DataViewGrid to your form<br \/>\n<\/h4>\n<p>\nAdd the datagrid to your form and create each of the columns.\n<\/p>\n<p>\nFor each column set the &#39;datapropertyname&#39; to match the class property you want to display in it.\n<\/p>\n<p>\nGo through the main properties and disable things like allow sort, allow resize etc assumign you don&#39;t want them.\n<\/p>\n<p>\nHeading row style \/ alignment etc<br \/>\nColumnHeadersDefaultStyle\n<\/p>\n<h4>\nDisplay It<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tarray&lt;GridEntry^&gt; ^GridData;\r\n\tGridData = gcnew array&lt;GridEntry^&gt;(10);\r\n\tGridData[0] = gcnew GridEntry(&quot;adam&quot;, 1, 1, 1, 1, 0);\r\n\tGridData[1] = gcnew GridEntry(&quot;nick&quot;, 0, 0, 0, 0, 1);\r\n\t...\r\n\r\n\tMyDataGridView1-&gt;DataSource = GridData;\r\n<\/code><\/pre>\n<h4>\nColumn Titles<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tdataGridView1-&gt;Columns[0]-&gt;HeaderText = &quot;Date \/ Time&quot;;\r\n\tdataGridView1-&gt;Columns[1]-&gt;HeaderText = &quot;Device No.&quot;;\r\n<\/code><\/pre>\n<h4>\nGet Data<br \/>\n<\/h4>\n<p>\nWhen using an array the array is updated as the user interacts ith it. When using a database as the datasource you have to manually tell it to be updated.\n<\/p>\n<h4>\nGetting Edit Events<br \/>\n<\/h4>\n<p>\nAll of the cell value type events only occur after editing of a cell is complete &#8211; yes all of them! When you say click a check box in a cell it begins an editing session. The value of the underlying cell doesn&#39;t change until that editing session ends. It&#39;s only when you click in another cell that the editing session ends and the cell value changes. The reason it is done this way is because while the user is in the process of editing the contents of a cell they may enter a value that is not valid. To avoid throwing errors because of this the grid waits until the entire value has been entered before propagating it to the underlying cell. That is the correct way to do things. If you want to force the cell value to be updated immediately when the user makes a change then you will have to write code to do this. You have to be careful with this though as a user can press escape to cancel any edits they&#39;ve made which won&#39;t cause an event. A simple solution is the have your processing code in the CellValueChanged event as normal and then to force it to occur by using the EndEdit event on the datagrid. For checkboxes you can simply use the mouse up event:\n<\/p>\n<pre>\r\n<code>\r\n\tprivate: System::Void MyDataGridView1_CellMouseUp(System::Object^  sender, System::Windows::Forms::DataGridViewCellMouseEventArgs^  e)\r\n\t{\r\n\t\t MyDataGridView1-&gt;EndEdit();\r\n\t}\r\n<\/code><\/pre>\n<p>\nAnd use this as the value changed event:\n<\/p>\n<pre>\r\n<code>\r\nprivate: System::Void MyDataGridView1_CellValueChanged(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)\r\n{\r\n\t\/\/Get the value of a cell\r\n\t\/\/value = MyDataGridView1-&gt;Rows(e-&gt;RowIndex)-&gt;Cells(e-&gt;ColumnIndex)-&gt;Value;\r\n<\/code><\/pre>\n<h4>\nAdding To or Modifying The Array<br \/>\n<\/h4>\n<p>\nNot sure this is the offficial best way to update the data grid view, but simply using this again&nbsp;does the job\n<\/p>\n<pre>\r\n<code>\r\n\tMyDataGridView-&gt;DataSource = MyArray;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good resources: http:\/\/www.devx.com\/dotnet\/Article\/33748 First Create A Class To Be Able To Add Data Create a class which has a variable for each column the datagrid will have &#8211; the class will be created for each data grid row. You can implement how these properties get set however you want, but a good way is to [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-datagridview"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=549"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":1371,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/549\/revisions\/1371"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}