Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在DataGrid控件内绑定DropDownList t Datatable列_Asp.net_Sql Server_Data Binding_Datagrid_Dropdownbox - Fatal编程技术网

Asp.net 在DataGrid控件内绑定DropDownList t Datatable列

Asp.net 在DataGrid控件内绑定DropDownList t Datatable列,asp.net,sql-server,data-binding,datagrid,dropdownbox,Asp.net,Sql Server,Data Binding,Datagrid,Dropdownbox,我得到了DataGrid控件,该控件从该DataGrid中的DataTable获取数据,我想将DropDownList控件与其DataTable中的相关数据绑定在一起 DropDownList commentDrop = (DropDownList)packageCommentDataGrid.FindControl("commentDrop"); commentDrop.DataSource = dt; commentDrop.DataTextField = dt

我得到了
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");

    }
}

谢谢,

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

第一名: 您必须将包含
DropDownList
GridView
列转换为
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();
}

实际上,我使用的是
DataGrid
控件,而不是
DataGridView
是的,我知道,但是GridView是DataGrid的继承者,更容易使用。您正在使用哪个Dotnet框架?然后我真的建议您使用GridView而不是DataGrid。但如果您打算继续使用DataGrid,那么我恐怕这个答案对您没有什么帮助。我找到你了,我会看看这个:)谢谢你,伙计