views:

544

answers:

4

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

A: 

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

ricky roy
A: 

you can use hidden control in the gridview to hidden the Primary keys

Ok lets say I have primary key of an entity I want to change. But how do I get the entity itself from primary key ?
drasto
A: 

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!

Pandincus
I did it other way bacause I use EntityDataSource but i Guess this would also work.
drasto
A: 

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)