【问题标题】:Bind DropDownList t Datatable Column inside DataGrid Control [closed]在DataGrid控件中绑定DropDownList t Datatable列[关闭]
【发布时间】:2012-06-15 04:02:41
【问题描述】:

我得到了 DataGrid 控件,该控件在此 DataGrid 中从 DataTable 获取其数据,我想将 DropDownList 控件与 DataTable 中的相关数据绑定

DropDownList commentDrop = (DropDownList)packageCommentDataGrid.FindControl("commentDrop");
       commentDrop.DataSource = dt;
        commentDrop.DataTextField = dt.Columns["CommentString"][0];
        commentDrop.DataValueField = dt.Columns["CommentP"][0];

ItemDataBound 事件将是这样的:

protected void packageCommentDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        DropDownList commentDrop = (DropDownList)e.Item.FindControl("commentDrop");

    }
}

谢谢,

【问题讨论】:

    标签: asp.net sql-server data-binding datagrid dropdownbox


    【解决方案1】:

    如果我理解正确,这就是你想要做的。

    第一: 您必须将具有DropDownListGridView 列转换为TemplateField。确保DropDownList<TemplateField><ItemTemplate><DropDownList id="" runat="server" /></ItemTemplate></TemplateField> 内。

    第二: 在后面的代码中创建 Gridview.RowDataBound 事件处理程序。然后在此方法中执行以下操作:

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl = (DropDownList)e.Row.Cells["Column Name / Index here"].FindControl("commentDrop");
        ddl.DataSource = dt;
        ddl.DataTextField = "Column Name";
        ddl.DataValueField = "Column Name";
        ddl.DataBind();
    }
    

    【讨论】:

    猜你喜欢
    • 2019-12-22
    • 1970-01-01
    • 2010-09-24
    • 2013-03-24
    • 2013-07-28
    • 2011-08-17
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多