asp.net - How to get selected value from GridView (C#) -


I have GridView which allows selection in EntityDataSource Data when the line is selected in GridView , how can I get the unit object selected? The primary key for entities is GridView .

Thanks for the reply

If you use a template field in your gridview If you are doing, you can pass the primary key to the commandarm properties for your selected commands. Example:

  & asp: TemplateField & gt; & Lt; ItemTemplate & gt; & Lt; Asp: LinkButton id = "btn select" runat = "server" text = "select" commandName = "select" command-execution = '& lt;% # Eval ("My_Primary_Key")% & gt; / & Gt; & Lt; / ItemTemplate & gt; & Lt; / ASP: TemplateField & gt;  

Then, when the user clicks the "Select" button, it turns on the "row command" event on your gridview. If you catch this event and check the e.CommandArgument property, you will be able to access the primary key associated with those selected rows:

  Protected MyFreeView_RowCommand (Object Sender, GridViewCommandEventArgs E) {if (e.CommandName.Equals ("Select", String Comparison.ContentCulture Ignor case)} {int primaryKeyInteger = Convert.ToInt32 (e.CommandArgument); // Other stuff ...}}  

Hope this helps!


Comments