How to get selected value from GridView (C#)
I have GridView
that allows select. It takes data fr开发者_StackOverflow中文版om 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!
You can either use the DataKeyNames field property of a grid view or you can also put the hidden value and bind the data on data bound event. Using hidden field Gridview selected event you search find hidden filed and get the value from there. Thanks Following link will help you.
http://forums.asp.net/t/951615.aspx
you can use hidden control in the gridview to hidden the Primary keys
you can use primary key goto the database search the record with the primary key .so then in the program use a class to receive the data.(c# is property)
精彩评论