Gridview中的下拉列表在编辑模式期间未填充

时间:2013-02-14 17:36:39

标签: asp.net vb.net gridview

我有一个asp gridview,我希望在所选行切换到编辑模式时填充下拉列表控件。

<EditItemTemplate>
      <asp:DropDownList ID="ddlFormatID" runat="server">
      </asp:DropDownList>

我google了一下,我知道你想在rowdatabound上做这个,然后检查一行是否正在编辑,如果是,那么填充DDL但我无法检查行工作正常:(

        If DataControlRowState.Edit = e.Row.RowState Then

        Dim ddlFormat As DropDownList = e.Row.FindControl("ddlFormatID")
        ddlFormat.DataSource = XRefBCWorker.GetFormatCombos
        ddlFormat.DataTextField = "Format"
        ddlFormat.DataValueField = "FormatID"
        ddlFormat.DataBind()

    End If

我做错了什么?

3 个答案:

答案 0 :(得分:1)

我能够找到这篇文章并且我在rowdatabound事件中更改了我的代码并且它有效!

        If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.DataItem IsNot Nothing Then
            If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
                Dim ddlFormat As DropDownList = e.Row.FindControl("ddlFormatID")
                ddlFormat.DataSource = XRefBCWorker.GetFormatCombos
                ddlFormat.DataTextField = "Format"
                ddlFormat.DataValueField = "FormatID"
                ddlFormat.DataBind()
                ddlFormat.SelectedIndex = CurrentFormatID
            End If
        End If
    End If

答案 1 :(得分:0)

您可以将DataItem强制转换为您显示的类型。

Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If DataControlRowState.Edit = e.Row.RowState Then
        Dim item = e.Row.DataItem
        Dim dr = DirectCast(item, DataRowView)
        Dim id = Integer.Parse(dr(0).ToString())
    End If
End Sub

答案 2 :(得分:0)

只需将其添加到标记中:

<asp:DropDownList SelectedValue='<%# Bind("categoryId") %>'