Format Your Brain

Linking DropDownList to GridView

The goal of this tutorial is to provide a search feature based on a DropDownList that will filter a GridView.

Setup

Before we start you will need the following:

  • Visual Studio 2010
  • SQL Server (any version will do)
  • Northwind Database (download the version associated with your SQL Server)

Since this tutorial is solely a programming tutorial it will not go over the downloading and installation of any of the above technologies.

DropDownList

  1. Open up a new Web Form page. I have created a page called Default.aspx.
  2. In your Tool Box view select DropDownList.

    image

  3. Rename your DropDownList ID to “ddlSearchCriteria.” It should now look like this in the source code view

    <asp:DropDownList ID=”ddlSearchCriteria” runat=”server”></asp:DropDownList>

  4. Now we will add a DataSource to the DropDownList by clicking on the arrow to the right of the DDL.

    image

  5. I don’t have a DataSource for the page so select <New data source…>/
  6. You will then be presented with the following screen. Select the Database option and then name your DataSource. I named mine SearchCriteriaDataSource. Click “Okay”.

    image

  7. I already have a ConnectionString set up named “NorthwindConnectionString.” Choose your connection string and click “Next.”
  8. You will now see a screen to set up the SELECT statement . For the Name I am using the “Categories” table. For the columns select CategoryID and CategoryName. Click “Next.”

    image

  9. In the next screen you can test your query. Once you know it works click “Finish.”
  10. To finish the DataSource configuration set up the “CategoryName” as the data field  to display. The “CategoryID” should be set as the data field value. Click “Okay.”

    image

  11. Run your website. You should now see the following in the browser.

    image

GridView

  1. Below the DropDownList insert a GridView control. You can select it from your Tool Box in the Data section.

    image

  2. Rename your GridView ID to be gvSearchResults

    <asp:GridView ID=”gvSearchResults” runat=”server”></asp:GridView>

  3. Now we will add the DataSource to the GridView in the same manner we added it to the DropDownList.
  4. Select the side arrow and choose the “<New data source… >“ (even though we have a data source already set, we will need a new one).

    image

  5. Name your data source SearchResultsDataSource. Click “Okay.”
  6. Select the NorthwindConnectionString. Click “Next.”
  7. For the Name I selected the Products table.
  8. For the columns I chose the ProductName and CategoryID.
  9. Now click on the “WHERE…” button on the right.

    image

  10. In the following screen you will need to select the Column as CategoryID. Select the Operator to be the “=” sign and then select  the Source to be “control.”
  11. The Parameter Properties will enable and in the Control ID box select our DropDownList, ddlSearchCriteria.

    image

  12. Click “Add.” This will add the new WHERE clause statement to the WHERE clause box. Click “Okay.”

    image

  13. Click “Next” on the original page of the “Configure the Select Statement” screen, then click “Finish.”
  14. Save you edits. If you run the web page you will notice that the query will show all the “Product Name” and the “Category ID” as 1. This is the default view.

    image

  15. If you select a different Category from the DropDownList you will notice that nothing happens. Select “Enable AutoPostBack” from the pop out arrow menu.

    image

  16. Save your page and test it.
  17. You will notice that every time you select a different category a new list appears with a new “Category ID.”

    image

Congratulations you have successfully linked a DropDownList to a GridView.

Bonus

If you want to have the category names appear instead of the category ID you will need to changed the DataSource query on the GridView to join the Categories Table. That is not part of this tutorial, but I have posted the SELECT statement below for your reference.

SELECT [ProductName], [CategoryName] FROM [Products] as p INNER JOIN [Categories] as c ON p.[CategoryID] = c.[CategoryID] WHERE (p.[CategoryID] = @CategoryID)

image

, , ,

Comments are currently closed.