Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 26-Jan-12 6:18am
v2

1 solution

look man i prefer to use like this example:
XML
<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
C#
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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900