I have a Gridview with template fields conatining drop down lists. I need to populate the dropdownlists with a sql statement. My gridview code is as such:

<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>

And the sql statement I need to populate it with is this:

SELECT V_VendorNo + '|' + V-VendorName FROM VendorTbl

Can anyone assist me with this?

有帮助吗?

解决方案

You will not to get the dropdownlist in RowDataBound event of grid and assign datasource to it and bind it.

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();     
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top