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.
|