Click here to Skip to main content
15,900,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please i a problem trying to get the primary key of a gridview control bounded by an entitydata source.I have a table called staff and this table has about 25 columns on it.The primary key of this table consist of a combination of two columns which is column [StaffID] and column [SchoolID].
I used the entitydatasource control to hook up to the table.I did not select all the columns i just selected four of the columns.Below is the markup of the entitydatasource control.
XML
<asp:EntityDataSource ID="EntityDataSource1" runat="server"

        ContextTypeName="DEMOWEBAPPLICATION.PriSecDbEntities" EnableFlattening="False"
        AutoGenerateOrderByClause="true"
        EntitySetName="Staffs" EntityTypeFilter="Staff"
        Select="it.[StaffID], it.[SchoolID], it.[Surname], it.[FirstName]">
        <OrderByParameters>
        <asp:Parameter
        DefaultValue="StaffID" />

        </OrderByParameters>
    </asp:EntityDataSource>

I bounded this to a gridview control.My question is i want a situation when i click on a row on the gridview i will be able to show the different columns from my staff table on different textbox control(for example i will be able to do something like this txtSurname.text=newStaff.name and so on) and i will be able to edit and updated it and it will updates on the database. I have been trying to create an object of staff and get the primary key column by doing this
VB
Dim newStaff As Staff = DirectCast(grdLoadStaffDetails.SelectedPersistedDataKey("StaffID"), Staff)
This is how i normally do it when using a datagrid with windows form application.
VB
Dim selectedUser = DirectCast(Me.grdLoadStaffDetails.CurrentRow.DataBoundItem, Staff)
I want the equivalent of this with a gridview control
in the gridview selectedindexchanged event handler but it throws up an invalid cast execption hour. I have been pulling out my hair out for the past five hours. Any help is highly appreciated. Thanks
Posted
Updated 28-Aug-13 23:29pm
v3

 
Share this answer
 
Protected Sub grdLoadStaffDetails_SelectedIndexChanged(sender As Object, e As EventArgs) Handles grdLoadStaffDetails.SelectedIndexChanged
       Dim Demo As New PriSecDbEntities
       Dim ke As String = grdLoadStaffDetails.SelectedPersistedDataKey("StaffID")
       Dim newStaff As Staff = Demo.Staffs.Where(Function(c) c.StaffID = ke).First

       txtSurname.Text = newStaff.Surname
       txtDOB.Text = newStaff.Usergroup
       txtFirstName.Text = newStaff.Password


   End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900