문제

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