Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2003, 12:18 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default Filtering a Grid

Guys,

I am trying to to use filtering to set up this grid so that it gets filtered depending on what is selected in the drop down box. The problem is, is that when ever you click on a beverage the filter does not work, or if you select a price the filter does not filter the other prices out! Any ideas...???

Heres my code: -

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>
<%@ Page Language="C#" Debug="true" %>

<html>
  <head>
  <title>DataGrid Filtering</title>
  </head>
  <body>
    <form runat="server">
      <asp:Literal id="IbCategoryFilter" runat="server" Text="Category:" />
      <asp:DropDownList id="ddlCategoryFilter" runat="server"
                        AutoPostBack = "True"
                        OnSelectedIndexChange = "FilterChange" />
      <asp:Literal id="IbPriceFilter" runat="server" Text="Price Range:" />
      <asp:DropDownList id="ddlPriceFilter" runat="server"
                        AutoPostBack = "True"
                        OnSelectedIndexChange = "FilterChange" >
      <asp:ListItem Value="0" Selected="True">Any Price</asp:ListItem>
      <asp:ListItem Value="1">Cheap</asp:ListItem>
      <asp:ListItem Value="2">Moderate</asp:ListItem>
      <asp:ListItem Value="3">Expensive</asp:ListItem>
      <asp:ListItem Value="4">Absurdly Expensive</asp:ListItem>
      </asp:DropDownList>
      <br/><br/>
      <asp:DataGrid id="dgProducts" runat="server">
        <HeaderStyle BackColor = "#C0C0FF"/>
        <ItemStyle BackColor = "#F1F1F1"/>
        <AlternatingItemStyle BackColor = "#E8E6E6" />
      </asp:DataGrid>
    </form>
  </body>
</html>

<script language = "c#" runat="server">

  public String strCategoryFilter = "CategoryID=1";
  public String strPriceFilter = "UnitPrice>0";

  private void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      FillDropDownList();
      DataFiller();
    }
  }

  private void FillDropDownList()
  {
    //Create the connection
    String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source = c:\\test\\Northwind.mdb;";
    OleDbConnection objConnection = new OleDbConnection(strConnection);
    String strSQLCategories = "SELECT CategoryName, CategoryID FROM Categories";

    //Create Command Object
    OleDbCommand objCommand = new OleDbCommand(strSQLCategories, objConnection);

    //OleDbDataReader objReader = null;

    objConnection.Open();
    //objReader = objCommand.ExecuteReader();

    ddlCategoryFilter.DataSource = objCommand.ExecuteReader();
    ddlCategoryFilter.DataTextField = "CategoryName";
    ddlCategoryFilter.DataValueField = "CategoryID";
    ddlCategoryFilter.DataBind();
    objConnection.Close();
  }

  private void DataFiller()
  {
    //Create the connection
    String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source = c:\\test\\Northwind.mdb;";
    OleDbConnection objConnection = new OleDbConnection(strConnection);
    String strSQLCategories = "SELECT ProductID, ProductName, CategoryID, UnitPrice FROM Products";

    //Create the adaptor
    OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQLCategories, objConnection);

    //Create Dataset and create new Datatable using the fill() method from the Adaptor object
    DataSet objDataSet = new DataSet();
    objAdapter.Fill(objDataSet, "dtProducts");

    DataView dvUK = new DataView(objDataSet.Tables["dtProducts"]);
    dvUK.RowFilter = strCategoryFilter + " AND (" + strPriceFilter + ")";

    dgProducts.DataSource = dvUK;
    dgProducts.DataBind();
  }

  private void FilterChange()
  {
    FilterByPrice(ddlPriceFilter.SelectedItem.Text.ToS tring());
    FilterByCategory(ddlCategoryFilter.SelectedItem.Te xt.ToString());
    DataFiller();
  }

  private void FilterByPrice(String strChoice)
  {
    switch (strChoice)
    {
      case "Any Price":
        strPriceFilter = "UnitPrice>0";
        break;
      case "Cheap":
        strPriceFilter = "UnitPrice < 20";
        break;
      case "Moderate":
        strPriceFilter = "UnitPrice > 19 AND UnitPrice <50 ";
        break;
      case "Expensive":
        strPriceFilter = "UnitPrice => 50";
        break;
      case "Absurdly Expensive":
        strPriceFilter = "UnitPrice > 100";
        break;
    }
  }

  private void FilterByCategory(String strChoice)
  {
    strCategoryFilter = "CategoryID = " + strChoice;
  }
</script>

__________________
Adz - Learning The J2EE Ways.
 
Old October 17th, 2003, 10:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try putting everything associated with the one event into a 'Sub'.
private void FilterChange()
  {
   strChoice = ddlPriceFilter.SelectedItem.Text.ToString()
   switch (strChoice)
    {
      case "Any Price":
        strPriceFilter = "UnitPrice>0";
        break;
      case "Cheap":
        strPriceFilter = "UnitPrice < 20";
        break;
      case "Moderate":
        strPriceFilter = "UnitPrice > 19 AND UnitPrice <50 ";
        break;
      case "Expensive":
        strPriceFilter = "UnitPrice => 50";
        break;
      case "Absurdly Expensive":
        strPriceFilter = "UnitPrice > 100";
        break;
    }

    //Create the connection
    String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source = c:\\test\\Northwind.mdb;";
    OleDbConnection objConnection = new OleDbConnection(strConnection);
    String strSQLCategories = "SELECT ProductID, ProductName, CategoryID, UnitPrice FROM Products";

    //Create the adaptor
    OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQLCategories, objConnection);

    //Create Dataset and create new Datatable using the fill() method from the Adaptor object
    DataSet objDataSet = new DataSet();
    objAdapter.Fill(objDataSet, "dtProducts");

    DataView dvUK = new DataView(objDataSet.Tables["dtProducts"]);
    dvUK.RowFilter = strCategoryFilter + " AND (" + strPriceFilter + ")";

    dgProducts.DataSource = dvUK;
    dgProducts.DataBind();

  }


Or you can debug using:

response.write(strSQLCategories)
Response.End()






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use OR during filtering..... raaj81 Reporting Services 0 July 30th, 2008 05:34 AM
Help with filtering xml jconroy XSLT 6 April 6th, 2008 11:33 PM
sorting and filtering oh my snafu7x7 XSLT 6 October 5th, 2007 10:54 AM
Count() and filtering asearle XSLT 1 December 4th, 2006 06:19 AM
Filtering a form [email protected] Access VBA 1 October 5th, 2004 03:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.