如果我在设计时通过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中的列
发布评论