c# - Get the Current Selected Row of GridView on a button Click -


I want to get DataKey selected GridView row on the button.

I want to do it in a template field like:

  & Lt; Asp: TemplateField & gt; & Lt; ItemTemplate & gt; / Edit: After a comment it is suggested that you pass RowIndex as a line idea that provides access to the whole line & lt; asp: LinkButton ID = "btnCopy" runat = "server" causes Validity = "False" CommandName = "MyCommandButton" CommandArgument = '& lt;% # Eval ( "MyDataKeyOrWhateverIwanteverIWantFromTheBindingSource")% & gt;' & Gt; & Lt; / ItemTemplate & gt; & Lt; / ASP: TemplateField & gt;  

CodeBehind

  MyCommandButton save zero (object sender, Kmmandarment e) {int DataKeyOrPK = int32.Parse ( E.CommandArgument .tar ()); }  

You will have other options:

  & asp: gridview id = "mygrid" runat = "server" width = 100% Detakanims = "Myid" Index selected on autogeneratecolumns = false = "MyAvent" & gt; & Lt; Asp: templatefield headertext = "select your dream home" & gt; & Lt; ItemTemplate & gt; & Lt; asp: linkbutton runat = "server" Command Name = "select" text = '& lt;% # Eval ( "whatever")% & gt;' / & Gt; & Lt; / ItemTemplate & gt; & Lt; / ASP: TemplateField & gt;  

Note the command name given above = "Select".

Data-bound controls recognize the names of some commands and automatically raise and handle suitable events for control. The names of the following commands are identified: Cancel, Delete, Edit, Insert, New, Page, Select, Sort and Update

codebehind < / Strong>

Private Zero MyAVent (Object Sender, EventAgds E) {string id = myGrid.SelectedDataKey.Value.ToString (); }

Comments