none
ASP.NET Filter EntityDataSource (WHERE clause) using a Drop Down RRS feed

  • Question

  • Hi,

    I am just getting my feet wet with Entity Framework.

    I have a GridView that is bound to an EntityDataSource.  However, I want to drive the filtering in the grid using a DropDown.

    My GridView is bound to the entity Employee.  Employee has an FK field to the Department entity (department_id).  The DropDown will contain a list of Department, with department_name being the Text and department_id being the Value.  However, I want to append the entry of All to the Department DropDown to show all Employees in the GridView.

    Is this possible to do?  Any examples or pointers?

    At the minute I am looking to do this in the DropDown.SelectedIndexChanged event:

    if (ddlDepartment.SelectedItem.Text.ToLower().Equals("all"))
    {
        EntityDataSource1.WhereParameters.Clear();
    }
    else
    {
        EntityDataSource1.WhereParameters.Add(new Parameter("d.department_id", System.Data.DbType.Int32, ddlDepartment.SelectedItem.Value));
    
    }
    
    EntityDataSource1.DataBind()

    Thanks,

    Andez


    Andez (Please mark as answer if it helps)


    • Edited by Andez Sunday, August 12, 2012 5:13 PM
    Sunday, August 12, 2012 4:56 PM

All replies

  • Hi Andez,

    Welcome to the MSDN forum.

    Could you please let me know if DropDown.SelectedIndexChanged event meet your requirements?

    If you still have any problem, please feel free to let me know.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, August 14, 2012 6:11 AM
  • Hi,

    If I read your code correctly then everytime you select a new value in the drop down it will append another where clause. Meaning that your drop down probably works the first time but not after that as it is now filtering for people with two departments, is that correct? If so then you probably need to clear the WhereParameters every time the drop down changes regardless of the value selected. Then, if it is not all add a new where clause.

    Having said that I'm not very familiar with this way of interacting with the model. So if what I said isn't correct then let me know and I will set up a test project to see if I can see what you are trying to do.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Tuesday, August 14, 2012 3:46 PM
    Moderator