{"id":159,"date":"2010-04-29T16:15:20","date_gmt":"2010-04-29T15:15:20","guid":{"rendered":"http:\/\/blog.rabihtawil.com\/?p=159"},"modified":"2010-04-29T16:15:20","modified_gmt":"2010-04-29T15:15:20","slug":"filtering-a-datagridview","status":"publish","type":"post","link":"http:\/\/www.raytawil.com\/?p=159","title":{"rendered":"Filtering a datagridview"},"content":{"rendered":"<p>assuming you have a datagridview that has data in it.<\/p>\n<p>you want to avoid recalling sql from the database &amp; just want to filter the datagrid.<\/p>\n<p>you can do the following, What you need (1 datagridview (data), 1 button (search), 1 textbox (string to search), 1 variable to precise the criteria your searching for)<\/p>\n<div id=\"wpshdo_1\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_1\" class=\"wp-synhighlighter-expanded\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_1\"><\/a><a id=\"wpshat_1\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_1\"  onClick=\"javascript:wpsh_toggleBlock(1)\" title=\"Click to show\/hide code block\">Source code<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_code(1)\" title=\"Show code only\"><img border=\"0\" style=\"border: 0 none\" src=\"http:\/\/www.raytawil.com\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_print(1)\" title=\"Print code\"><img border=\"0\" style=\"border: 0 none\" src=\"http:\/\/www.raytawil.com\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"http:\/\/www.raytawil.com\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img border=\"0\" style=\"border: 0 none\" src=\"http:\/\/www.raytawil.com\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_1\" class=\"wp-synhighlighter-inner\" style=\"display: block;\"><pre class=\"vbnet\" style=\"font-family:monospace;\"><span class=\"kw6\">Dim<\/span> findcrit <span class=\"kw2\">as<\/span> <span class=\"kw2\">string<\/span>\n<span class=\"kw6\">Dim<\/span> dt <span class=\"kw2\">As<\/span> DataTable <span class=\"sy0\">=<\/span> Ds_Pos.<span class=\"me1\">Employees<\/span>\n <span class=\"kw6\">Dim<\/span> dv <span class=\"kw2\">As<\/span> <span class=\"kw2\">New<\/span> DataView<span class=\"br0\">&#40;<\/span>dt<span class=\"br0\">&#41;<\/span>\n&nbsp;\n <span class=\"kw6\">Dim<\/span> _RowFilter <span class=\"kw2\">As<\/span> <span class=\"kw2\">String<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;&quot;<\/span>\n <span class=\"kw6\">Dim<\/span> _FieldType <span class=\"sy0\">=<\/span> dt.<span class=\"me1\">Columns<\/span><span class=\"br0\">&#40;<\/span>findcrit<span class=\"br0\">&#41;<\/span>.<span class=\"me1\">DataType<\/span>.<span class=\"me1\">ToString<\/span>\n&nbsp;\n <span class=\"kw6\">Select<\/span> <span class=\"kw6\">Case<\/span> _FieldType\n <span class=\"kw6\">Case<\/span> <span class=\"st0\">&quot;System.Int32&quot;<\/span>\n _RowFilter <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;convert(&quot;<\/span> <span class=\"sy0\">&amp;<\/span> findcrit <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;, 'System.String') like '&quot;<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"kw2\">Me<\/span>.<span class=\"me1\">TextBox1<\/span>.<span class=\"me1\">Text<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;'&quot;<\/span>\n <span class=\"kw6\">Case<\/span> <span class=\"st0\">&quot;System.Int64&quot;<\/span>\n _RowFilter <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;convert(&quot;<\/span> <span class=\"sy0\">&amp;<\/span> findcrit <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;, 'System.String') like '&quot;<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"kw2\">Me<\/span>.<span class=\"me1\">TextBox1<\/span>.<span class=\"me1\">Text<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;'&quot;<\/span>\n <span class=\"kw6\">Case<\/span> <span class=\"st0\">&quot;System.Double&quot;<\/span>\n _RowFilter <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;convert(&quot;<\/span> <span class=\"sy0\">&amp;<\/span> findcrit <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;, 'System.String') like '&quot;<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"kw2\">Me<\/span>.<span class=\"me1\">TextBox1<\/span>.<span class=\"me1\">Text<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;'&quot;<\/span>\n <span class=\"kw6\">Case<\/span> <span class=\"st0\">&quot;System.String&quot;<\/span>\n _RowFilter <span class=\"sy0\">=<\/span> findcrit <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot; like '&quot;<\/span> <span class=\"sy0\">&amp;<\/span> TextBox1.<span class=\"me1\">Text<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"st0\">&quot;*'&quot;<\/span>\n <span class=\"kw6\">End<\/span> <span class=\"kw6\">Select<\/span>\n dv.<span class=\"me1\">RowFilter<\/span> <span class=\"sy0\">=<\/span> _RowFilter\n EmployeesDataGridView.<span class=\"me1\">DataSource<\/span> <span class=\"sy0\">=<\/span> dv<\/pre><\/div><\/div>\n<p>The cases are to cast the type of variable from any type to string because you can only use the method on string data types.<\/p>\n<p>the idea is to move the content to a data table then filter then return it back to the datatable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>assuming you have a datagridview that has data in it. you want to avoid recalling sql from the database &amp; just want to filter the datagrid. you can do the following, What you need (1 datagridview (data), 1 button (search), 1 textbox (string to search), 1 variable to precise the criteria your searching for) The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[110],"tags":[33,34,37,75,78,131],"_links":{"self":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/159"}],"collection":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=159"}],"version-history":[{"count":0,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.raytawil.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}