grabbing the entity from a row in a gridview

Posts   
 
    
nilsey
User
Posts: 54
Joined: 11-Jan-2008
# Posted on: 31-Mar-2008 19:52:24   

i am trying to select multiple rows from a gridview using a checkbox in a template column. The gridview is bound to an Entity Collection (of QuestionGroupCollection in my case) in t he codebehind (not using a LLBLGenProdDatadsource, but just in the codebehind since i need some complex filtyering to happen first).


<asp:GridView ID="GridView_Results" runat="server" DataKeyNames="Id"
    AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <HeaderStyle  HorizontalAlign="Left" /> 
            <ItemStyle BackColor="Gainsboro"/>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="CheckBox_SelectQuestionGroup" Text="Select" />
                <asp:Label runat="server" ID="QuestionGroupName" Text='<%# Bind("Name") %>'></asp:Label>
                <br />
                <asp:GridView runat="server" ID="GridView_QuestionDetail" DataSource='<%# Eval("Question") %>'></asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Name" HeaderText="Question Group" SortExpression="Name" />
    </Columns>
</asp:GridView>
<asp:Button ID="Button_UseSelectedGroups" runat="server" Text="Use Selected Groups" />

when the button is pressed i want to find the selected rows and save those entities into a new collection of selected entities:


    Protected Sub Button_UseSelectedGroups_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_UseSelectedGroups.Click
        ' get the groups selected and add them top the collection we will use
        For Each row As GridViewRow In GridView_Results.Rows
            If Not row.DataItem Is Nothing Then
                Dim qg As QuestionGroupEntity = CType(row.DataItem, QuestionGroupEntity)
                Dim cb As CheckBox = CType(FindControlRecursive(row, "CheckBox_SelectQuestionGroup"), CheckBox)
                If cb.Checked Then
                    SelectedQuestionGroups.Add(qg)
                End If
            End If
        Next
    End Sub

the prblem is that the row.DataItem is always null (Nothing).

How do I get at the Entity (QuestionGroupEntity) from a row in a gridview bound to an EntityCollection (QuestionGroupCollection). Wheni step through the process and get to the button clicked event, i can't find the data items at all in the gridview.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Apr-2008 05:32:34   

When you bind a collection to a webform control, the collection is gone when the page is done, so when you get a postback, the actual bound object isn't there. You have to store it in the viewstate or in the session for example. You also could use LLBLGenProDataSource and pass to it your complicated filter at the page load.

David Elizondo | LLBLGen Support Team
nilsey
User
Posts: 54
Joined: 11-Jan-2008
# Posted on: 01-Apr-2008 17:46:10   

What are advantages to using the LLBLGenProDataSource as opposed to instantiating an entity collection in the codebehind?

Will the collection be persisted in viewstate through the LLBLGenProDataSource or will i have to set that up in viewstate in the codebehind as well?

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 01-Apr-2008 21:27:04   

What are advantages to using the LLBLGenProDataSource as opposed to instantiating an entity collection in the codebehind?

Basically you obtain data bounding behaviors out of the box. See the documentation for details.

Will the collection be persisted in viewstate through the LLBLGenProDataSource or will i have to set that up in viewstate in the codebehind as well?

Yes, LLBLGenProDataSource would handle all the basic CRUD behaviors by itself.