在datagridview标头中添加过滤的最简单方法

编程入门 行业动态 更新时间:2024-10-26 02:37:33
本文介绍了在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标头中添加过滤的最简单方法

本文发布于:2023-11-09 15:59:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   方法   datagridview   标头中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!