{"id":42,"date":"2009-06-10T03:03:04","date_gmt":"2009-06-10T01:03:04","guid":{"rendered":"http:\/\/dirk.forbiddendream.be\/?p=42"},"modified":"2016-01-07T10:50:33","modified_gmt":"2016-01-07T10:50:33","slug":"fill-a-datagridview-from-a-dataset-code-wise","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=42","title":{"rendered":"Fill a DataGridView from a DataSet (code-wise)"},"content":{"rendered":"<p>Today I&#8217;m goin to explain how you can show your data from a table inside a DataGridView.<\/p>\n<p>We&#8217;ll run a simple SELECT command against our database, fill our dataset and then display the contents of our dataset in our datagridview<\/p>\n<p>First off we&#8217;ll start by defining the objects we need:<\/p>\n<p><!--more--><\/p>\n<pre name=\"code\" class=\"vb.net\">'Variable declaration, scope: global\r\n'Note: MyDatabase is your database. In this example the database will be located where the *.exe is located of our program\r\nDim g_oCon As New OleDb.OleDbConnection(\"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MyDatabase.accdb;Persist Security Info=False;\")\r\nDim g_oSelect As New OleDb.OleDbCommand(\"\", oCon)  'Our SELECT command\r\nDim g_oDR As OleDb.OleDbDataReader 'Our data reader\r\nDim g_oDA As New OleDb.OleDbDataAdapter(\"\", oCon) 'Our data adapter\r\nDim g_oDS As New DataSet 'Our dataset<\/pre>\n<p>Now that we have these, we can get started.<br \/>\nI&#8217;ll create a seperate sub where we can do all the work:<\/p>\n<pre name=\"code\" class=\"vb.net\">'Subroutine to fill our dataset and show it's contents in our datagridview\r\nPrivate Sub m_fnShowData()\r\n\tTry\r\n\t\t'Set our query\r\n\t\tg_oSelect.CommandText = \"SELECT * FROM MyTable\"\r\n\r\n\t\t'Open up the connection\r\n\t\tg_oCon.Open()\r\n\r\n\t\t'Set the select command for our data adapter\r\n\t\tg_oDA.SelectCommand = g_oSelect\r\n\r\n\t\t'Fill the dataset\r\n\t\tg_oDA.Fill(g_oDS, \"MyTable\")\r\n\r\n\t\t'Set what the datagridview has to display\r\n\t\tg_oDGV.DataSource = g_oDA.Tables(\"MyTable\")\r\n\r\n\t\t'Close the connection\r\n\t\tg_oCon.Close()\r\n\tCatch ex As OleDbExeption\r\n\t\tMessageBox.Show(ex.ToString)\r\n\tEnd Try\r\nEnd Sub<\/pre>\n<p>And there we go, a DataGridView filled with the exact same data as found in our table<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I&#8217;m goin to explain how you can show your data from a table inside a DataGridView. We&#8217;ll run a simple SELECT command against our database, fill our dataset and then display the contents of our dataset in our datagridview First off we&#8217;ll start by defining the objects we need:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/42"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":2,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":790,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions\/790"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}