introlux
asked on
Sample For GridView with Multiple Filters ASP.NET C#
I have come across this web site:
http://www.aspdotnetcodes.com/Asp.Net_GridView_Filter.aspx
Sample: http://www.aspdotnetcodes.com/Asp.Net_GridView_Filter_Sample.aspx
I would like to sort out my gridview like the example I have shown.
Bare in mind, I have been using MS Access as my database. C# as my background code.
Any idea??
Thanks
http://www.aspdotnetcodes.com/Asp.Net_GridView_Filter.aspx
Sample: http://www.aspdotnetcodes.com/Asp.Net_GridView_Filter_Sample.aspx
I would like to sort out my gridview like the example I have shown.
Bare in mind, I have been using MS Access as my database. C# as my background code.
Any idea??
Thanks
ASKER
I have looked at that exmaple and I have no idea how to go around doing that on my code. If you read the comments, people are having problems with it aswell.
I have started doing one filter but it is different to what I have done, and got helped on. I would like to use multiple filters instead of using one.
This is my code as follows:
I have started doing one filter but it is different to what I have done, and got helped on. I would like to use multiple filters instead of using one.
This is my code as follows:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class testFilter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void FilterDropDown_IndexChanged(object sender, EventArgs e)
{
DropDownList dd = (DropDownList)sender;
ViewState["SelectedValue"] = dd.SelectedValue;
this.AccessDataSource1.SelectCommand = "SELECT [PName], [ICD_IDF], [PType], [CName] FROM [tblProCom] WHERE [PName] <> 'select' AND [PType]='" + dd.SelectedValue + "'";
GridView1.DataBind();
}
protected void SetValue(object sender, EventArgs e)
{
if (ViewState["SelectedValue"] != null)
{
DropDownList ddl = (DropDownList)sender;
ddl.SelectedValue = ViewState["SelectedValue"].ToString();
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("testFilter.aspx");
}
}
=====================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testFilter.aspx.cs" Inherits="testFilter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/Data/ProCom.mdb" SelectCommand="SELECT [PName], [ICD_IDF], [PType], [CName] FROM [tblProCom] WHERE [PName] <> 'select'">
</asp:AccessDataSource>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Reset Filters" /><br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="AccessDataSource1"
ForeColor="#333333" GridLines="None"
AllowPaging="True" OnPageIndexChanged="GridView1_PageIndexChanged">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="PName" HeaderText="PName" SortExpression="PName" />
<asp:BoundField DataField="ICD_IDF" HeaderText="ICD_IDF" SortExpression="ICD_IDF" />
<asp:TemplateField HeaderText="ProjectType" SortExpression="PType">
<HeaderTemplate>
Project Type
<asp:DropDownList ID="ddPType"
DataTextField="PType"
AutoPostBack="true"
OnSelectedIndexChanged="FilterDropDown_IndexChanged"
OnPreRender="SetValue"
DataSourceID="AccessDataSource2" runat="server" AppendDataBoundItems="True">
<asp:ListItem>Please select</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PType") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CName" HeaderText="CName" SortExpression="CName" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<br />
<br />
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/Data/ProCom.mdb"
SelectCommand="SELECT DISTINCT PType FROM tblProCom">
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
ASKER
any idea?
Sorry, haven't had time to set up my own test yet...might have some time this morning...
just looking at what you have so far though:
protected void FilterDropDown_IndexChange d(object sender, EventArgs e)
{
DropDownList dd = (DropDownList)sender;
ViewState["SelectedValue"] = dd.SelectedValue;
this.AccessDataSource1.Sel ectCommand = "SELECT [PName], [ICD_IDF], [PType], [CName] FROM [tblProCom] WHERE [PName] <> 'select' AND [PType]='" + dd.SelectedValue + "'";
GridView1.DataBind();
}
I wouldn't try the set the select command and rebind here...the only thing I'd do in the IndexChanged event(s) (since you'll eventually have multiple filters)...the only thing I'd do is set the viewstate value....
then setting the select command and binding in page_load based on viewstate values...
just looking at what you have so far though:
protected void FilterDropDown_IndexChange
{
DropDownList dd = (DropDownList)sender;
ViewState["SelectedValue"]
this.AccessDataSource1.Sel
GridView1.DataBind();
}
I wouldn't try the set the select command and rebind here...the only thing I'd do in the IndexChanged event(s) (since you'll eventually have multiple filters)...the only thing I'd do is set the viewstate value....
then setting the select command and binding in page_load based on viewstate values...
ASKER
When will you get the time to give it a test run? as im not sure how to approach this.
Regards,
Regards,
How important to you is it to have the dropdownlists in the headers autopostback?
I have it working (using Northwind as test db) when I have "Apply" and "Undo" filter as buttons outside the grid...
ScreenShot8-7-2008-9.30.41-AM.jpg
ScreenShot8-7-2008-9.31.00-AM.jpg
I have it working (using Northwind as test db) when I have "Apply" and "Undo" filter as buttons outside the grid...
ScreenShot8-7-2008-9.30.41-AM.jpg
ScreenShot8-7-2008-9.31.00-AM.jpg
ASKER
That could work aswell i guess
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks for the advice!
The article is pretty straightfoward....notice that you don't have to change your queries or anything.
the filtering happens to the dataview:
if (condition!= null)
dv.RowFilter = condition;
so if you implement the dropdownlists in the header as described in the tutorial, and the data you return is in a dataset or datatable, you can make a dataview out of that data and apply the "RowFilter" to the dataview...