我得到了DataGrid
从 DataTable
获取数据的控件在这个 DataGrid 中我想绑定(bind) 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");
}
}
谢谢
最佳答案
如果我理解正确的话,这就是你想要做的。
第一:
您必须将 GridView
转换为具有 DropDownList
的列至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();
}
关于asp.net - 在 DataGrid 控件内绑定(bind) DropDownList t 数据表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993834/