在Gridview中将数据绑定到Dropdownbox [英] Binding Data to Dropdownbox in Gridview

查看:63
本文介绍了在Gridview中将数据绑定到Dropdownbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,

我有要求,在Gridview中显示Dropdownbox中的记录。

Dropdownbox数据来自不同的查询。

如果我的gridview有50行,那么每次触发查询时都会列出下拉框并且需要时间加载。

这不是要列出的静态数据,它是也是动态的。

请指导这是更好的选择。

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.

推荐答案

你的aspx页面包含如下:



your aspx page contains follows :

<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 :

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;
    }
}


由于您需要花费大量时间来加载大型动态数据,因此您可能需要考虑使用AJAX jQuery。



请参阅使用jQuery AJAX在ASP.NET中绑定下拉列表 [ ^ ]。



这将帮助您解决问题。



-KR
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


使用gridview的RowDataBound事件并绑定其中的下拉列表。

- Harish
Use RowDataBound event of the gridview and bind the dropdown inside it.
- Harish


这篇关于在Gridview中将数据绑定到Dropdownbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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