{"id":54,"date":"2018-02-26T14:41:57","date_gmt":"2018-02-26T18:41:57","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=54"},"modified":"2019-02-06T23:59:14","modified_gmt":"2019-02-07T03:59:14","slug":"get-data-from-selected-row-or-cell-in-gridview","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/get-data-from-selected-row-or-cell-in-gridview\/","title":{"rendered":"ASP.Net: Get Data from Selected Row or Cell in GridView"},"content":{"rendered":"<p>When you need to pull data from a GridView, e.g. when the user selects the row and you want to populate<br \/>\nsome fields that match data from the selected row, it is pretty common to simply reference the cell<br \/>\nthat was clicked on.  If you used a normal databound column, you could call the Text property of the cell.<\/p>\n<pre><code class=\"language-vb\">' Select the row somehow (multiple ways depending on what you're doing)\nTextBox1.Text = row.Cells[1].Text\n<\/code><\/pre>\n<p>If you used a TemplateField, you can simply look for the ID of the control containing the value, cast it,<br \/>\nand extract the value.<\/p>\n<pre><code class=\"language-vb\">' Select the row somehow (multiple ways depending on what you're doing)\nTextBox1.Text = CType(row.FindControl(\"litHolderType\"), Literal).Text\n<\/code><\/pre>\n<p>If you are looking for the primary key(s) for the given row, the best way to do it is to first set<br \/>\nthe DataKeyNames property on the GridView, then reference it in the code-behind.<br \/>\nPopulate the key(s) as follows:<\/p>\n<pre><code class=\"language-vb\">&lt;asp:GridView ID=\"gvHolders\" runat=\"server\" DataKeyNames=\"Emplid,TypeID\"  .... &gt;\n<\/code><\/pre>\n<p>Reference the datakeys in code-behind as follows (e.g. RowEditing event in this example):<\/p>\n<pre><code class=\"language-vb\">Dim iEmplid As String = gvHolders.DataKeys(e.NewEditIndex).Values(\"ID\")\nDim iTypeID As Integer = gvHolders.DataKeys(e.NewEditIndex).Values(\"TypeID\")\n<\/code><\/pre>\n<hr \/>\n<h2>Alternate Option<\/h2>\n<p>You can bypass the built-in GridView edit\/select functionality by adding a button with manually-specified parameters that trigger a custom function in the code-behind, with a variable passed through as a <code>CommandArgument<\/code>.<\/p>\n<p>GridView Column:<\/p>\n<pre><code>&lt;asp:TemplateField&gt;\n    &lt;ItemTemplate&gt;\n        &lt;asp:LinkButton ID=\"btnDoSomething\" runat=\"server\" CommandName=\"DoSomething\" \n            CommandArgument=\"&lt;%#Eval('id')%&gt;\" OnCommand=\"DoSomething\" CssClass=\"item-edit\" \n            ToolTip=\"Edit Holder\" data-toggle=\"tooltip\" data-placement=\"right\"&gt;\n            &lt;i class=\"fa fa-pencil-square-o\" aria-hidden=\"true\"&gt;&lt;\/i&gt;\n        &lt;\/asp:LinkButton&gt;\n    &lt;\/ItemTemplate&gt;\n&lt;\/asp:TemplateField&gt;\n<\/code><\/pre>\n<p>Code-Behind Function:<\/p>\n<pre><code class=\"language-vb\">Protected Sub DoSomething(Sender As Object, e As CommandEventArgs)\n    ' Access command name through e.CommandName\n    ' Access command argument through e.CommandArgument\nEnd Sub\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you need to pull data from a GridView, e.g. when the user selects the row and you want to populate some fields that match data from the selected row,  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[9,11,10],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-asp-net","tag-asp-net","tag-asp-net-gridview","tag-vb-net"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-S","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":64,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/54\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}