GridView TemplateField绑定到SQL语句?

时间:2012-06-06 17:37:07

标签: asp.net sql gridview drop-down-menu templatefield

我有一个带有模板字段的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

有人可以帮助我吗?

1 个答案:

答案 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();     
    }
}