在datagridview标头中添加过滤的最简单方法 [英] easiest way to add filtering in a datagridview header

查看:73
本文介绍了在datagridview标头中添加过滤的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,它动态绑定到sql db.在某些标题中,我需要过滤.我知道如何在标头中获取ddl,但是我还没有想出向其添加过滤功能的最简单方法.可悲的是,我已经为此奋斗了大约一个月,但没有一个适用的解决方案出现.如果有人知道这样做的简单方法,我将不胜感激.

在此先感谢
Sean8084

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

推荐答案

我在这里可能还很遥远,但我还是要给它一个机会.

如果将网格绑定到sql db,是否使用DataSet,DataTable进行绑定?
我有一个使用DataGrid的WinForms应用程序,并使用DefaultView
绑定了网格. 可以通过多种方式对DataTable和视图进行排序,过滤和操作.
您不能使用视图进行过滤吗?

view.Sort
view.RowFilter
view.Select(选择返回DataRow [],但不能将其用于绑定)
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)


设计页面:
< asp:GridView
ID ="gvCity" runat =服务器"
AutoGenerateColumns ="False" Width ="100%" AllowPaging ="True"
ShowFooter ="True" AllowSorting ="True"
onpageindexchanging ="gvCity_PageIndexChanging" onsorting ="gvCity_Sorting"><列>
< 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"
高度="20px" ImageUrl =〜/Images/saveicon.jpg"宽度="20px"/>
</FooterTemplate>
</asp:TemplateField>
< asp:TemplateField HeaderText ="CityId" SortExpression ="CityId">< ItemTemplate>
< asp:标签ID ="lblCityId" runat =服务器" 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:标签ID ="lblECityName" runat =服务器"
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:标签ID ="lblLCityName" runat =服务器"
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:标签ID ="lblStateName" runat =服务器" Text =''<%#Eval("EStateName")%>''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
< asp:TemplateField HeaderText ="StateId" Visible ="False">
< ItemTemplate>
< asp:标签ID ="lblStateId" runat =服务器" Text =''<%#Eval("StateId")%>''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>< AlternatingRowStyle CssClass ="gridAltRow"></AlternatingRowStyle>< HeaderStyle CssClass ="gridheader"/>< PagerStyle
CssClass ="gridheader"/>< RowStyle CssClass ="gridrow" Horizo​​ntalAlign ="Center"></RowStyle></asp:GridView>
背后的代码:
受保护的void gvCity_Sorting(对象发送者,GridViewSortEventArgs e)
{
DataTable dt =(DataTable)Session ["dtFilter"];
字符串sortdir ="DESC";
如果(ViewState ["SortDir"]!= null)
{
sortdir = ViewState ["SortDir"].ToString();
如果(sortdir =="ASC")
{
e.SortDirection = SortDirection.Descending;
ViewState ["SortDir"] ="DESC";
}
其他
{
e.SortDirection = SortDirection.Ascending;
ViewState ["SortDir"] ="ASC";
}
}
其他
{
ViewState ["SortDir"] ="DESC";
}
dt =(新的DataView(dt,",e.SortExpression +" + ViewState ["SortDir"].ToString(),DataViewRowState.CurrentRows)).ToTable();
gvCity.DataSource = dt;
gvCity.DataBind();
FillFooterDDlState();
}
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();
}


这篇关于在datagridview标头中添加过滤的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆