Fanda
Fanda

Reputation: 3786

Filtered DropDownList in GridView

I have DropDownList nested in GridView. How can I filter DropDownList data source, to show only active db rows (column Active as bit type) OR currently selected value of the DropDownList?

Where part of following code doesn't work. Is there some way how to bound to curent GridView row data (by the markup)?

<asp:GridView ID="GridView" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PlasticTypeId" 
    DataSourceID="dsPlasticTypes">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:TemplateField HeaderText="<%$ Resources:Labels, PlasticFamily %>" SortExpression="PlasticFamily.Name">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("PlasticFamily.Name") %>'/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlFamilies" runat="server" DataSourceID="dsFamilies" DataValueField="PlasticFamilyId"
                    DataTextField="Name" SelectedValue='<%# Bind("PlasticFamilyId") %>'/>
                <asp:EntityDataSource ID="dsFamilies" runat="server" 
                    ConnectionString="name=PlasticsDbEntities" OrderBy="it.Name"
                    DefaultContainerName="PlasticsDbEntities" EnableFlattening="False" 
                    EntitySetName="PlasticFamilies"
                    Where="it.Active==true || it.PlasticFamilyId==@control">
                    <WhereParameters>
                        <asp:ControlParameter Name="control" ControlID="ddlFamilies" PropertyName="SelectedValue" Type="Int32" />
                    </WhereParameters>
                </asp:EntityDataSource>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Upvotes: 0

Views: 786

Answers (1)

Fanda
Fanda

Reputation: 3786

I have solved this by putting a hidden field above the DropDownList binded to the same field and using it as ControlParameter to filter data source.

<asp:TemplateField HeaderText="<%$ Resources:Labels, PlasticFamily %>" SortExpression="PlasticFamily.Name">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("PlasticFamily.Name") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:HiddenField ID="hfPlasticFamilyId" runat="server" Value='<%# Eval("PlasticFamilyId") %>' />
        <asp:DropDownList ID="ddlFamilies" runat="server" DataSourceID="dsFamilies" 
            DataTextField="Name" DataValueField="PlasticFamilyId" SelectedValue='<%# Bind("PlasticFamilyId") %>' />
        <asp:EntityDataSource ID="dsFamilies" runat="server" 
            ConnectionString="name=PlasticsDbEntities" 
            DefaultContainerName="PlasticsDbEntities" EnableFlattening="False" 
            EntitySetName="PlasticFamilies" OrderBy="it.Name" 
            Where="it.Active==true || it.PlasticFamilyId==@control">
            <WhereParameters>
                <asp:ControlParameter ControlID="hfPlasticFamilyId" Name="control" 
                    PropertyName="Value" Type="Int32" />
            </WhereParameters>
        </asp:EntityDataSource>
    </EditItemTemplate>
</asp:TemplateField>

Upvotes: 1

Related Questions