我有一个带有模板字段的Gridview,其中包含下拉列表。我需要使用sql语句填充下拉列表。我的gridview代码是这样的:
<asp:TemplateField HeaderText="Ledger">
<EditItemTemplate>
<asp:DropDownList ID="ddlItemTempLedger" runat="server" Width="61px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Width="75px" />
</asp:TemplateField>
我需要填充它的sql语句是:
SELECT V_VendorNo + '|' + V-VendorName FROM VendorTbl
有人可以帮助我吗?
答案 0 :(得分:3)
您不会在网格的RowDataBound事件中获取下拉列表并为其分配数据源并将其绑定。
protected void GrdViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlItemTempLedger = e.Row.FindControl("ddlItemTempLedger ") as DropDownList ;
ddlItemTempLedger.DataSource = dt; //DataTable from database
ddlItemTempLedger.DataTextField = "FieldForTextInDataTabledt";
ddlItemTempLedger.DataValueField = "FieldForValueInDataTabledt";
ddlItemTempLedger.DataBind();
}
}