gridview的rowdatabound

时间:2009-07-07 04:20:31

标签: asp.net vb.net gridview

我在rowdatabound fn中使用以下代码。

Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.Footer Then
            Dim ddlItem As DropDownList = CType(e.Row.FindControl("ddlFProjectLevels"), DropDownList)
            If ddlItem IsNot Nothing Then
                ddlItem.DataSource = objMileStone.GetProjectLevels()
                ddlItem.DataValueField = "MileStoneID"
                ddlItem.DataTextField = "Name"
                ddlItem.DataBind()
            End If
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowState = DataControlRowState.Edit Then
                Dim ddlEProjectLevels As DropDownList = CType(e.Row.FindControl("ddlEProjectLevels"), DropDownList)
                ddlEProjectLevels.DataSource = objMileStone.GetProjectLevels()
                ddlEProjectLevels.DataValueField = "MileStoneID"
                ddlEProjectLevels.DataTextField = "Name"
                ddlEProjectLevels.DataBind()
                ddlEProjectLevels.SelectedValue = milestoneid
            End If
        End If


    End Sub

ddlEProjectLevels是edititemtemplate中的下拉列表。我点击第一行中的编辑ddlEProjectLevels从数据库中加载数据。但是在第二行下拉列表中不包含值。在第三行中它从备用行中的db.Means加载,当我点击编辑下拉列表(ddlEProjectLevels)不加载值。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:5)

与其他控件相比,此问题有点混乱,适用于GridView和DetailsView控件。您需要使用按位逻辑检查RowState枚举,因为项目可能具有Alternate或Edit状态。

所以而不是:

If e.Row.RowState = DataControlRowState.Edit Then

尝试:

If (e.Row.RowState And DataControlRowState.Edit) > 0 Then

有关详情,请查看DataControlRowState Enumeration页面。