在运行时将过滤器功能添加到datagridview中的列 [英] add filter capability to a column in the datagridview at runtime

查看:83
本文介绍了在运行时将过滤器功能添加到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中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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