Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Dear Friends,
I have requirement, as to display the records in Gridview with Dropdownbox in it.
The Dropdownbox data is load from different query.
If my gridview has 50 rows, then each time it fire the query to get the dropdownbox listed and it takes time to load.
It is not static data to be listed, it is also dynamic.
Kindly guide be the better option for this.
Posted

your aspx page contains follows :

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <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>






Backend page :

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
 
v2
As you're having large dynamic data that takes time to load, you might want to consider using AJAX with jQuery.

See this Bind Dropdownlist in ASP.NET using jQuery AJAX[^].

This would help you to get through your problem.

-KR
 
Share this answer
 
Use RowDataBound event of the gridview and bind the dropdown inside it.
- Harish
 
Share this answer
 
Download your data into some sort of list object and bind the drop down to the list with your data.
 
Share this answer
 
I have created a global dataset and stored the data on page load.
Later i have used the same dataset by C# Filter option. It is fast and my problem is solved.
It connects to SQL only on page load.
 
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