asp.net - 在 DataGrid 控件内绑定(bind) DropDownList t 数据表列

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

我得到了DataGridDataTable 获取数据的控件在这个 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/

相关文章:

asp.net - IIS URL 重写 Default.aspx 而不是 Slash

c# - ASP.NET TextBox - 是否可以使用行内代码 <% %> 初始化文本属性

c# - 使用 asp.net c# 从 ListView 中的选定项目中获取值

c# - 没有真正的 OOP 或对象的动态表单?

sql-server - 在 SQL Server 中将因数转换为十进制

sql - 按日期与空组分组

asp.net - WPF 中 ASP.NET DataBind() 的替代方案是什么?

c# - Microsoft Visual Studio 表单应用程序 - mysql 错误

sql - 使用 int 或 newsequentialid 作为聚簇表主键有哪些优点/缺点?

wpf - 在当前 ItemsSource 上下文之外绑定(bind)到 DataContext