为GridView中的DropDownList赋值

时间:2023-12-21 16:29:44

<Bda:GridView ID="gvMessage" runat="server" Height="70px" Width="100%" OnRowDataBound="gvMessage_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="单号">
<HeaderStyle HorizontalAlign="Center" Width="15%" />
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Width="120px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="类型">
<HeaderStyle HorizontalAlign="Center" Width="20%" />
<ItemTemplate>
<asp:DropDownList ID="drpType" runat="server" Width="120px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</Bda:GridView>

后台代码

protected void BindType()
{
for (int i = 0; i < gvMessage.Rows.Count; i++)
{
DropDownList drplist = (DropDownList)gvMessage.Rows[i].Cells[1].FindControl("drpType");
drplist.DataTextField = "DataLabel";
drplist.DataValueField = "Code";
drplist.DataSource = 查到的数据源;
drplist.DataBind();
drplist.Items.Insert(0, new ListItem("请选择", ""));
}
}