Question : GridView-FormView Master-Detail Setup (Programmatic Solution)

Hello Experts!

I need some assistance in setting up a gridview/formview detail/master solution. I have written most of what I'd like to accomplish thus far through code and would prefer to stick to this route for this as well. So far I have created a dataset function that I use to bind my gridivew control and populate the controls in my itemtemplate setup.

Now, my best assumption here is that in order to create this relationship, I would first get the CommandArgument to obtain the Datakey for the selected row index. Then pass the value as a parameter to another separate dataset specifically for the formview [detail] and bind the formview control to the dataset. I'm not sure if I'm making any sense or if I'm even headed in the right direction here. Can someone offer up some guidance on whether this is the best, most effecient approach in creating this master/detail relationship??? Any info, code samples, guidance, etc. on how to setup the master/detail rel. through code would be greatly appreciated!!

Here's what I've done so far....
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
**********
CODE BEHIND (.VB)
**********

DataSet used to bind to GV
==========================

    Protected Function GetNewHireRequests(ByVal status As Integer) As DataSet

        Dim NHcmd As New SqlCommand("usp_NewHireStatus", conn)
        NHcmd.CommandType = Data.CommandType.StoredProcedure
        NHcmd.Parameters.AddWithValue("@approvalMgrID", Data.SqlDbType.Int)
        NHcmd.Parameters("@approvalMgrID").Value = CInt(Session("ApprovingMgrID"))
        NHcmd.Parameters.AddWithValue("@status", Data.SqlDbType.Int)
        NHcmd.Parameters("@status").Value = status

        Dim da As SqlDataAdapter
        Dim ds2 As New DataSet

        da = New SqlDataAdapter(NHcmd)
        ds2 = New DataSet()
        da.Fill(ds2)

        Return ds2

    End Function


GridView Bind
=================
    Protected Sub gv_GetApprovedNewHireRequests()

        If GetNewHireRequests(approvedStatusID).Tables(0).Rows.Count > 0 Then
            gv_ApprovalNewHires.AutoGenerateColumns = False
            gv_ApprovalNewHires.DataSource = GetNewHireRequests(approvedStatusID)
            gv_ApprovalNewHires.DataBind()
        End If

    End Sub

gv_RowCommand Event
======================

    Protected Sub gv_ApprovalNewHires_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_ApprovalNewHires.RowCommand

        If e.CommandName = "Select" Then

            Dim row_index As Integer = e.CommandArgument.ToString()
            Dim SelectedEmpID As String = gv_ApprovalNewHires.DataKeys(row_index)(0).ToString()

            'do something to bind fv control here...???

        End If

    End Sub


***********************
.ASPX
***********************

GridView - Master
==============
                            
                                
                                    
                                    
                                        
                                         asp:Label ID="pending_lblJobTitle" runat="server" Text='<%# Eval("JOB_TITLE")%>'>
                                        
                                    


FormView - Detail
==============
                            
                            
                                ">
                            
                            

Answer : GridView-FormView Master-Detail Setup (Programmatic Solution)

Yes thats the way to go. You get the Data, based on the Selected item in the GV and bind the FV.
The only change I would suggest is since you are using GridViewCommand event you can make use of GridViewSelectedIndexChanged event instead of RowCommand event.
Random Solutions  
 
programming4us programming4us