Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview which is bound dynamically to a sql db. in some of the headers, I need to filter. I know how to get a ddl in the header, but I haven't figured out the easiest way to add the filtering functionality to it. Sadly, I've been fighting with this for about a month and not a single applicable solution has come up. if anyone knows a simple way of doing this, I would greatly appreciate it.

Thanks in advance
Sean8084
Posted

I may be way off here but I'm going to give it a shot anyway.

If you are binding the grid to a sql db are you using DataSet, DataTable for binding?
I have a WinForms application that uses a DataGrid and I bind the grid using the DefaultView
of the DataTable and views can be sorted, filtered and manipulated in many ways.
Can you not use a view to do your filtering?

view.Sort
view.RowFilter
view.Select (The select returns DataRow[] but you can't use it for binding)
 
Share this answer
 
Design Page :
<asp:GridView
ID="gvCity" runat="server"
AutoGenerateColumns="False" Width="100%" AllowPaging="True"
ShowFooter="True" AllowSorting="True"
onpageindexchanging="gvCity_PageIndexChanging" onsorting="gvCity_Sorting"><Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CausesValidation="False"
ImageUrl="~/Images/edit.png" onclick="btnEdit_Click" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="btnSave" runat="server" onclick="btnSave_Click"
Height="20px" ImageUrl="~/Images/saveicon.jpg" Width="20px" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CityId" SortExpression="CityId"><ItemTemplate>
<asp:Label ID="lblCityId" runat="server" Text='<%# Eval("CityId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCityId" runat="server" Enabled="False"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CityName" SortExpression="ECityName">
<ItemTemplate>
<asp:Label ID="lblECityName" runat="server"
Text='<%# Eval("ECityName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtECityName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CityName(Ar)" SortExpression="LCityName">
<ItemTemplate>
<asp:Label ID="lblLCityName" runat="server"
Text='<%# Eval("LCityName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLCityName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StateName" SortExpression="EStateName">
<FooterTemplate>
<asp:DropDownList ID="ddlEStateName" runat="server">
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblStateName" runat="server" Text='<%# Eval("EStateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StateId" Visible="False">
<ItemTemplate>
<asp:Label ID="lblStateId" runat="server" Text='<%# Eval("StateId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns><AlternatingRowStyle CssClass="gridAltRow" ></AlternatingRowStyle><HeaderStyle CssClass="gridheader" /><PagerStyle
CssClass="gridheader" /><RowStyle CssClass="gridrow" HorizontalAlign="Center" ></RowStyle></asp:GridView>
Code Behind :
protected void gvCity_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = (DataTable)Session["dtFilter"];
string sortdir = "DESC";
if (ViewState["SortDir"] != null)
{
sortdir = ViewState["SortDir"].ToString();
if (sortdir == "ASC")
{
e.SortDirection = SortDirection.Descending;
ViewState["SortDir"] = "DESC";
}
else
{
e.SortDirection = SortDirection.Ascending;
ViewState["SortDir"] = "ASC";
}
}
else
{
ViewState["SortDir"] = "DESC";
}
dt = (new DataView(dt, "", e.SortExpression + " " + ViewState["SortDir"].ToString(), DataViewRowState.CurrentRows)).ToTable();
gvCity.DataSource = dt;
gvCity.DataBind();
FillFooterDDlState();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900