在运行时将过滤器功能添加到datagridview中的列

编程入门 行业动态 更新时间:2024-10-28 02:30:38
本文介绍了在运行时将过滤器功能添加到datagridview中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我在设计时通过datagridview的属性将列手动添加到DataGridView,则每个列都有一个称为ColumnType的属性.如果将其设置为DataGridViewAutoFilterTextBoxColumn,则在运行时,您可以单击datagridview列标题上的下拉列表,并按以下各项之一过滤数据. 当我在运行时填充数据和列时,显然我不能使用设计视图来设置列类型. 所以我想知道如何使其中一列具有这种过滤功能? 谢谢

if I add the columns to the DataGridView manually at design time through the properties of the datagridview, there is a property of each column which is called ColumnType. This if set to DataGridViewAutoFilterTextBoxColumn, then at runtime, you can click on the dropdown on the header of the datagridview column and filter the data by one of the items. As I am populating the data and the columns at runtime, then obviously I can not use the design view to set columntypes. So I would like to know how to make one of the columns to have this filter capability? Thanks

推荐答案

看我喜欢用这个例子的人: look man i prefer to use like this example: <asp:TemplateField HeaderText="Car Name" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label runat="server" Text='<%# Eval("CarName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="SearchCarName" runat="server"> </asp:DropDownList> </FooterTemplate> <FooterStyle HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField>

并为网格和下拉菜单提供相同的数据源 并在后面的代码中使用它来绑定数据

and give the same datasource to grid and to dropdown and in the code behind use this to bind the data

<pre lang="c#"> List<string> lstSearchCarName = new List<string>(); lstSearchCarName.Add("--Search Here--"); lstSearchCarName.AddRange(lstCars.Select(p => p.CarName).Distinct().ToList());</string></string>

<pre lang="c#"> DropDownList ddlSearchCarName = gvCarsForSale.FooterRow.FindControl("SearchCarName") as DropDownList; ddlSearchCarName.DataSource = lstSearchCarName; ddlSearchCarName.DataBind();

还可以在任何列内或新列中添加搜索按钮

also add a search button inside any coloumn or in a new column

<pre lang="c#"> <footertemplate> <asp:linkbutton id="btnSearch" runat="server" onclick="btnSearch_Click" forecolor="DarkRed" xmlns:asp="#unknown"> Text="Search" CommandName="Search"></asp:linkbutton> </footertemplate>

并在后面的代码中使用此事件

and in the code behind use this event

protected void btnSearch_Click(object sender, EventArgs e) { lstCars = (List<car>)Session["lstCars"]; DropDownList ddlSearchCarName = gvCarsForSale.FooterRow.FindControl("SearchCarName") as DropDownList; DropDownList ddlSearchNumberOfSeats = gvCarsForSale.FooterRow.FindControl("SearchNumberOfSeats") as DropDownList; DropDownList ddlSearchModel = gvCarsForSale.FooterRow.FindControl("SearchModel") as DropDownList; string SearchCarName = ddlSearchCarName.SelectedItem.ToString(); string SearchNumberOfSeats = ddlSearchNumberOfSeats.SelectedValue; string SearchModel = ddlSearchModel.SelectedValue; List<car> lst = new List<car>(); if (SearchCarName != "--Search Here--") { lst = lstCars.FindAll(p => p.CarName == SearchCarName); lstCars = lst; lst = null; } if (SearchNumberOfSeats.ToString() != "--Search Here--") { lst = lstCars.FindAll(p => p.NumberOfSeats.ToString() == SearchNumberOfSeats); lstCars = lst; lst = null; } if (SearchModel.ToString() != "--Search Here--") { lst = lstCars.FindAll(p => p.Model.ToString() == SearchModel); lstCars = lst; lst = null; } BindGridAndDropDownLists(lstCars); }</car></car></car>

希望对您有帮助,这是一个很好的方法...

hope that help you this is very nice way...

更多推荐

在运行时将过滤器功能添加到datagridview中的列

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

发布评论

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

>www.elefans.com

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