Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Inside asp.net gridview i have a DropDownList like this

XML
<asp:DropDownList ID="ddlTrimName" AutoPostBack="true" runat="server" CssClass="content-grd"
ClientIDMode="Static" OnSelectedIndexChanged="ddlTrimName_SelectedIndexChanged">                  </asp:DropDownList>


This will fill in page load. Now I want to add a filter. when user type records needs to be filter. is there any method in jquery to do this?
Posted
Updated 22-Sep-14 19:21pm
v2
Comments
[no name] 23-Sep-14 1:23am    
Question is not clear.
Gihan Liyanage 23-Sep-14 1:44am    
You have one dropdownlist for the Grid view ? or do you have dynamic drop downs for all rows. What you mean by user type records, Where the user writes ??
amnk.info 23-Sep-14 1:54am    
this is my gridview.

<asp:GridView ID="dtgPackingGrid" runat="server" AutoGenerateColumns="false" CssClass="mGridSec"
AllowPaging="false" Width="100%" DataKeyNames="RowID,PackingDtlId,PackingCostSheetId"
OnRowCreated="dtgPackingGrid_RowCreated" OnRowCommand="dtgPackingGrid_RowCommand"
OnRowDataBound="dtgPackingGrid_RowDataBound" OnPageIndexChanging="dtgPackingGrid_PageIndexChanging">
<alternatingrowstyle cssclass="alt">
<columns>
<asp:TemplateField HeaderText="Sub Group">
<itemtemplate>
<asp:DropDownList ID="ddlSubGroup" runat="server" ClientIDMode="Static" CssClass="content-grd"
AutoPostBack="true" OnSelectedIndexChanged="ddlSubGroup_SelectedIndexChanged">


<HeaderStyle Font-Bold="true" Font-Names="Tahoma" Font-Size="12px" ForeColor="White"
Width="100px" Horiz/>
<itemstyle horiz="">

<asp:TemplateField HeaderText="Packing Type">
<itemtemplate>
<asp:DropDownList ID="ddlPackingName" runat="server" CssClass="content-grd" AutoPostBack="true"
ClientIDMode="Static" OnSelectedIndexChanged="ddlPackingName_SelectedIndexChanged">


<HeaderStyle Font-Bold="true" Font-Names="Tahoma" Font-Size="12px" ForeColor="White"
Width="100px" Horiz/>
<itemstyle horiz="">






I want to add a filter to ddlSubGroup DropDownList.


as example if DropDownList have
-sunday
-Monday
-Tuesday

when user type in the textbox Sunday DropDownList shows Sunday

1 solution

SQL
HTML Markup

The HTML Markup contains an ASP.Net GridView with DropDownList in ItemTemplate of TemplateField.


C#
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" onrowdatabound="OnRowDataBound" xmlns:asp="#unknown">
    <columns>
        <asp:boundfield headertext="Name" datafield="ContactName" />
        <asp:templatefield headertext="Country">
            <itemtemplate>
                <asp:label id="lblCountry" runat="server" text="<%# Eval("Country") %>" visible="false" />
                <asp:dropdownlist id="ddlCountries" runat="server">
                </asp:dropdownlist>
            </itemtemplate>
        </asp:templatefield>
    </columns>
</asp:gridview>

 

Binding the GridView

The below code binds the ASP.Net GridView with records from the Customers table of the Northwind database using C#


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers");
        GridView1.DataBind();
    }
}
 
private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row
        DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
        ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
        ddlCountries.DataTextField = "Country";
        ddlCountries.DataValueField = "Country";
        ddlCountries.DataBind();

        //Add Default Item in the DropDownList
        ddlCountries.Items.Insert(0, new ListItem("Please select"));
          
        //Select the Country of Customer in DropDownList
        string country = (e.Row.FindControl("lblCountry") as Label).Text;
        ddlCountries.Items.FindByValue(country).Selected = true;
    }
}
 
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