Brad Mc
Brad Mc

Reputation: 151

EntityDataSource Filtering

This is a little painful, I feel like this should be really easy.

I have a EntityDataSource as such:

<asp:EntityDataSource ID="TaskDataSource" runat="server" ConnectionString="name=ScheduleEntities" DefaultContainerName="ScheduleEntities" EnableFlattening="False" EntitySetName="TaskItems" EnableInsert="True" EnableUpdate="True" OrderBy="it.Duration" Where="it.deleted = False">

And I can't for the life of me get it to show me only the not deleted results. The OrderBy works and everything is fine...I just can't get it to filter.

Upvotes: 0

Views: 263

Answers (2)

Brad Mc
Brad Mc

Reputation: 151

I got it working by making a linqDataSource with a filter made in the UI and took the filter format, here is the code:

<asp:EntityDataSource ID="TaskDataSource" runat="server" ConnectionString="name=ScheduleEntities" DefaultContainerName="ScheduleEntities" EnableFlattening="False" EntitySetName="TaskItems" EnableInsert="True" EnableUpdate="True" OrderBy="" Where="deleted == @deleted">
    <WhereParameters>
        <asp:Parameter DefaultValue="False" Name="deleted" Type="Boolean" />
    </WhereParameters>
</asp:EntityDataSource>

BUT, I couldn't end up using this because I needed to use the dynamic filters on the grid also, so my workaround was to hide rows during the binding process(if there is a more efficient way please update me!) with this:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    var editItem = e.Item as GridEditFormItem;
    if (e.Item is GridDataItem && !e.Item.IsInEditMode)
    {
        GridDataItem item = (GridDataItem)e.Item;
        bool success,deleted;
        bool.TryParse(item["deleted"].Text, out success);
        if (success)
        {
            deleted = bool.Parse(item["deleted"].Text);
            if (deleted)
            {
                item.Display = false;
            }
        }
    }
}

Upvotes: 0

Ron
Ron

Reputation: 1828

you need to make AutoGenerateWhereClause property to true in the EntityDataSource,

<asp:EntityDataSource ID="TaskDataSource" runat="server" ConnectionString="name=ScheduleEntities" DefaultContainerName="ScheduleEntities" EnableFlattening="False" EntitySetName="TaskItems" EnableInsert="True" EnableUpdate="True" OrderBy="it.Duration" Where="it.deleted = False" AutoGenerateWhereClause="true">

Upvotes: 0

Related Questions