I have GridView
that allows select. It takes data from EntityDataSource
. How do I get the entity Object that is selected when the row in GridView
is selected ?
Primary keys of entities are not displayed in the GridView
.
Thanks for answers
If you're using a template field in your gridview, you can pass the primary key in the CommandArgument property for your select command. Example:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnSelect" runat="server" Text="Select" CommandName="select" CommandArgument='<%# Eval("My_Primary_Key") %>' />
</ItemTemplate>
</asp:TemplateField>
Then, when the user clicks the "select" button, this triggers a "RowCommand" event on your gridview. If you capture this event and check the e.CommandArgument property, you'll be able to gain access to the primary key corresponding to the row they selected:
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("select", StringComparison.CurrentCultureIgnoreCase))
{
int primaryKeyInteger = Convert.ToInt32(e.CommandArgument);
// Do other stuff ...
}
}
Hope this helps!