Search code examples
c#asp.netfilteringsqldatasource

Filtering SqlDataSource with multiple dropdownlists


I have an SqlDataSource at the moment, which I want to be able to be filtered using 3 DropDownList's. Basically, each DropDownList contains values for a certain column on the table, so when they choose one of the values, the SqlDataSource only shows values with that value in the relevant column.

So my table is somewhat like this:

ID (primary key)

ManufacturerID

ItemTypeID

UnitTypeID

And the 3 DropDownList's contain the following:

Manufacturers (value = ManufacturerID)

ItemTypes (value = ItemTypeID)

UnitTypes (value = UnitTypeID)

My filter expression for the datasource is currently like this, but it doesn't seem to work unless all 3 boxes have a value selected, which is not ideal. Any ideas guys? My filterexpression is below:

ItemTypeID = {0} OR ManufacturerID = {1} OR UnitTypeID = {2} OR (ItemTypeID = {0} AND ManufacturerID = {1}) OR (ItemTypeID = {1} AND UnitTypeID = {2}) OR (ManufacturerID = {1} AND UnitTypeID = {2}) OR (ItemTypeID = {0} AND ManufacturerID = {1} AND UnitTypeID = {2})

Solution

  • You can add the filter criteria one by one like the following:

    // Idea not code..

    yourFilterExpression = "";
    if (dropDownList1.SelectedValue != null)
     filterExpression = // filter expression 1
    if (dropDownList2.SelectedValue != null)
     filterExpression += // filter expression 2
    

    Good luck!