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,那么我恐怕这个答案对您没有什么帮助。我找到你了,我会看看这个:)谢谢你,伙计