填充gridview中的下拉列表

时间:2017-06-16 14:40:05

标签: asp.net gridview model-binding dropdown

我希望有人可以帮助我。我有一个gridview,里面每行都有一个下拉列表。我希望根据行键值的值填充下拉列表。

<asp:GridView ID="gvCart" runat="server" CssClass="table table-striped" DataKeyNames="UOMSCHDL" OnRowDataBound="gvCart_RowDataBound"
    SelectMethod="gvCart_GetData" AutoGenerateColumns="false" DeleteMethod="gvCart_DeleteItem" AutoGenerateDeleteButton="true">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>Item #</HeaderTemplate>
            <ItemTemplate>
                <%#Eval("ITEMNMBR")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>UOM</HeaderTemplate>
            <ItemTemplate>
                <asp:DropDownList ID="ddlUOM" runat="server"AutoPostBack="true" SelectMethod="getUOM" CssClass="dropdown-toggle"
                                            DataTextField="UOFM" DataValueField="UOFM" AppendDataBoundItems="true">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

以下是填充下拉列表的代码:

Public Function getUOM() As IEnumerable
    'the following code has a red sqiggly under RowIndex because it isn't 
    'declared. I get that, but what do I use to get the RowIndex

    Dim uom As String = gvCart.DataKeys(RowIndex).Value.ToString()

    'this query works if I "hardcode" the where to = "roll" but I want 
    ' it to be dynamic

    Dim query = From i In db.IV40202
                Where i.UOMSCHDL = uom ' "roll"
                Order By i.SEQNUMBR
    Return query.ToList()

End Function

正如我在代码中评论的那样,我不知道如何获取RowIndex。

先谢谢你的帮助。

我想我会为有兴趣的人发布完整的解决方案。

<asp:TemplateField>
    <HeaderTemplate>UOM</HeaderTemplate>
        <ItemTemplate>
            <asp:DropDownList ID="ddlUOM" runat="server" CssClass="dropdown-toggle"></asp:DropDownList>
         </ItemTemplate>

并在后面的代码中:

 Public Function getUOM(uom As String) As IEnumerable
    Dim myUOM As String = Trim(uom)
    'ultimately this needs to be based on the uomschdl of each item
    Dim query = From i In db.IV40202
                Where i.UOMSCHDL = myUOM ' "roll"
                Order By i.SEQNUMBR
                Select New With {
                    .UOFM = i.UOFM
                    }
    Return query.ToList()

End Function

Protected Sub gvCart_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim uom As String = gvCart.DataKeys(e.Row.RowIndex).Value.ToString()
        Dim ddlUOM As DropDownList = TryCast(e.Row.FindControl("ddlUOM"), DropDownList)
        ddlUOM.DataSource = getUOM(uom)
        ddlUOM.DataTextField = "UOFM"
        ddlUOM.DataValueField = "UOFM"
        ddlUOM.DataBind()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您希望将对getUOM(rowIndex)的调用移到gvCart_RowDataBound处理程序中,如下所示:

protected Sub gvCart_RowDataBound(Dim sender As Object, Dim e As GridViewRowEventArgs)
    Dim rowIndex As Int = e.Row.RowIndex
    getUOM(rowIndex)        
End Sub