怎样在gridview中显示下拉框中的对应内容
1个回答
展开全部
显示下拉框中的对应内容 原理是 在GridView绑定数据后在进行绑定 DropDownList 这样才能显示下拉框的内容,代码如下:
--前台代码:
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="下拉框">
<ItemTemplate>
<asp:DropDownList ID="CallInIdSelControl" runat="server" Visible="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
--后台代码
/// <summary>
/// 处理GridView行绑定
/// </summary>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//找到DropDownList 控件,然后进行绑定或者追加内容
DropDownList _CallInIdSelControl = (DropDownList)e.Row.FindControl("CallInIdSelControl");
//new ListItem(text值,value值):追加内容形式
_CallInIdSelControl.Items.Add(new ListItem("第一","1"));
//绑定内容形式
//_CallInIdSelControl.DataSource = 数据源;
//_CallInIdSelControl.DataTextField = "绑定text字段";
//_CallInIdSelControl.DataValueField= "绑定value字段";
}
}
--前台代码:
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="下拉框">
<ItemTemplate>
<asp:DropDownList ID="CallInIdSelControl" runat="server" Visible="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
--后台代码
/// <summary>
/// 处理GridView行绑定
/// </summary>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//找到DropDownList 控件,然后进行绑定或者追加内容
DropDownList _CallInIdSelControl = (DropDownList)e.Row.FindControl("CallInIdSelControl");
//new ListItem(text值,value值):追加内容形式
_CallInIdSelControl.Items.Add(new ListItem("第一","1"));
//绑定内容形式
//_CallInIdSelControl.DataSource = 数据源;
//_CallInIdSelControl.DataTextField = "绑定text字段";
//_CallInIdSelControl.DataValueField= "绑定value字段";
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询