{"id":178,"date":"2010-07-17T20:21:41","date_gmt":"2010-07-18T01:21:41","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=178"},"modified":"2011-08-21T07:28:01","modified_gmt":"2011-08-21T12:28:01","slug":"asp-net-gridview-all-in-one-quick-reference","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2010\/07\/17\/asp-net-gridview-all-in-one-quick-reference\/","title":{"rendered":"ASP.NET GridView All-In-One Quick Reference"},"content":{"rendered":"<p>Here is a quick collection of snippets where one can quickly lookup the necessities of setting up a GridView for the purposes of displaying information to the viewer through a database (for example, MySQL):<\/p>\n<p><strong>Adding Columns of Data<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nDataTable dt = new DataTable(&quot;Tablename&quot;);\r\ndt.Columns.Add(&quot;Column1&quot;);\r\ndt.Columns.Add(&quot;Column2&quot;);\r\ndt.Columns.Add(&quot;Column3&quot;);\r\n\/\/ for every row\r\nDataRow dr = dt.NewRow();\r\ndr[&quot;Column1&quot;] = &quot;Testing&quot;;\r\ndr[&quot;Column2&quot;] = &quot;Blah&quot;;\r\ndr[&quot;Column3&quot;] = &quot;Big Blah&quot;;\r\ndt.Rows.Add(dr);\r\n<\/pre>\n<p><strong>Binding the Data (and storage across sessions)<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nDataView dv = new DataView(dt);\r\nSession[&quot;data&quot;] = dv;\r\ngridviewctrl.DataSource = dv;\r\ngridviewctrl.DataBind();\r\n<\/pre>\n<p><strong>Paging<\/strong><br \/>\nNote: This needs AllowPaging to be set to &#8220;true&#8221;. The following code goes in the &#8220;PageIndexChanging&#8221; callback.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ngridviewctrl.PageIndex = e.NewPageIndex;\r\ngridviewctrl.DataSource = Session[&quot;data&quot;];\r\ngridviewctrl.DataBind();\r\n<\/pre>\n<p><strong>Sorting<\/strong><br \/>\nNote: This needs AllowSorting to be set to &#8220;true&#8221; and ensure all columns you want sorted have a &#8220;SortExpression&#8221; that matches the column name in the DataTable. The following code goes in the &#8220;Sorting&#8221; callback.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring dir;\r\nif (e.SortDirection == SortDirection.Ascending) {\r\n    dir = &quot;ASC&quot;;\r\n} else {\r\n    dir = &quot;DESC&quot;;\r\n}\r\n((DataView)Session[&quot;Data&quot;]).Sort = e.SortExpression + &quot; &quot; + dir;\r\ngridviewctrl.DataSource = Session[&quot;data&quot;];\r\ngridviewctrl.DataBind();\r\n<\/pre>\n<p><strong>&lt;asp:ButtonField&gt;s<\/strong><br \/>\nCreate a &lt;asp:ButtonField&gt; declaration as follows: &lt;asp:ButtonField ButtonType=&#8221;Button&#8221; Text=&#8221;Button Text Here&#8221; CommandName=&#8221;DoSomething&#8221; \/&gt;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (e.CommandName == &quot;DoSomething&quot;) {\r\n    \/\/ get Index\r\n    int index = Convert.ToInt32(e.CommandArgument);\r\n    string test = ((DataView)Session[&quot;Data&quot;]).Table.Rows[index][&quot;Column1&quot;];\r\n    Response.Redirect(&quot;http:\/\/www.google.com\/search?q=&quot; + test.ToString()&quot;);\r\n}\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Here is a quick collection of snippets where one can quickly lookup the necessities of setting up a GridView for the purposes of displaying information to the viewer through a database (for example, MySQL): Adding Columns of Data Binding the Data (and storage across sessions) Paging Note: This needs AllowPaging to be set to &#8220;true&#8221;. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,4],"tags":[681,680,289,697,700,696,689,698,699],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/178"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}