Click here to Skip to main content
Click here to Skip to main content

Adding Show All option to a DropDownList GridView data filter with LinqDataSource

, 31 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to add Show all option a the DropDownList used as a data filter for a GridView with no code-behind on a LinqDataSource.

Introduction

This is a very quick way (kind of a workaround) to implement a "Show all" option to a dropdownlist filter of a GridView, through a LinqDataSource, using the Visual Studio Designer mode.

In this example, we are using LINQ to SQL entity classes (shown in the picture below) which are contained in a .dbml file named VehiclesData.dbml. The vehicle models are going to be listed in the GridView and filtered with the dropdownlist by its manufacturer.

Alternate Text

Setting up the GridView

  1. Add a LinqDataSource control to the GridView.
    • Expand the GridView's 'Smart Tasks' and choose New Data Source.
    • Alternate Text

    • For data type, select LINQ (we'll name it GridViewDataSource in this example).
    • Alternate Text

    • Click OK.
  2. Configure the GridViewDataSource to use the VehicleModels table.
    • Expand the 'Smart Tasks' of the GridView
    • GridView SmartTasks

    • Choose the appropriate DataContext (in our case, VehiclesDataDataContext), click Next.
    • Select the VehicleModel table.
    • Note: to have the Edit, Update, Delete functions automatically available in the GridView, all fields from the table must be selected.

      VehicleModel table

    • Click Finish.

We'll setup the GridView to show only the Name column and allow sorting and paging.

Alternate Text

Setting up the DropDownList

  1. Configure the DropDownList to list the Manufacturer column from the VehicleManufacturers table:
    • Add another LinqDataSource (with name DropDownDataSource here).
    • Expand the 'Smart Tasks' of the DropDownDataSource, select Configure Data Source, choose VehiclesDataDataContext.
    • Now select the VehicleManufacturers table, with only the Manufacturer and ID columns. We need the Manufacturer column to show it in the list and the ID to bind everything here together.
    • Alternate Text

  2. Configure the DropDownList to show the data in the Manufacturer column.
    • Open the 'Smart Tasks' of the DropDownList, select Choose Data Source.
    • Choose the DropDownDataSource. For the data field (this is shown on the list), choose Manufacturer, and the data value, we need the ID.
    • Alternate Text

Setup the DropDownList to filter the data displayed in the GridView

  1. Setup the 'Where' clause of the GridViewDataSource.
    • Open the 'Smart Tasks' of the GridViewDataSource, select Configure Data Source, click Next, and then Where.
    • Alternate Text

    • VehicleManufacturerID is the foreign key in the VehicleModels table, which needs to be associated to the ID column from the VehicleManufacturer table. "How to" is shown in the picture below:
    • Alternate Text

    • Click Add.
    • Click OK.
    • Set AutoPostBack="true" to the DropDownList.

Up until now, we have everything working as expected, but the GridView shows only the filtered selection from the DropDownList.

Adding 'Show All' option to the DropDownList filter

  1. Add the 'Show All' list item:
    • Expand 'Smart Tasks' of the DropDownList, select Edit Items.
    • Add new item, name it 'Show All', and for value, put -1.
    • Alternate Text

    • Click OK.
    • Set AppendDataBoundItems="true" to the DropDownList.
    • Go to the GridViewDataSource source and make the changes like shown below:
    • <asp:linqdatasource id="GridViewDataSource" runat="server" 
         contexttypename="WebApplication1.VehiclesDataDataContext"
         tablename="VehicleModels" 
         where="(VehicleManufacturerID == 
           @VehicleManufacturerID) || (@VehicleManufacturerID == @ShowAll)">
      <WhereParameters>
          <asp:ControlParameter ControlID="ddManufacturers" Name="VehicleManufacturerID" 
              PropertyName="SelectedValue" Type="Int32" />
          <asp:Parameter Name="ShowAll" Type="Int32" DefaultValue="-1"/>
      </WhereParameters>
      </asp:linqdatasource>

So what happens is that we add a helper parameter to extend the Where clause of the LINQ query.

where="(VehicleManufacturerID
== @VehicleManufacturerID) || (@VehicleManufacturerID == @ShowAll)"

with the help of the additional parameter:

<asp:Parameter Name="ShowAll" Type="Int32" DefaultValue="-1" />

But remember that its value must never have a value that the @VehicleManufacturerID or whatever parameter we equal to it, might have.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

zdrsh
Software Developer (Senior)
Macedonia, The Republic Of Macedonia, The Republic Of
No Biography provided
Follow on   Twitter

Comments and Discussions

 
SuggestionSQLDatasource - Solution PinmemberRyan Gillies3-Mar-13 18:14 
QuestionSQL datasource Pingroupkolisa10-Jul-12 4:19 
QuestionThis code gives me an empty gridview instead of all Pinmemberbongo475-Jun-12 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150123.1 | Last Updated 31 Oct 2011
Article Copyright 2011 by zdrsh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid