Vb.net – Rowdatabound of gridview


I am using following code in rowdatabound function.

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"
            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.SelectedValue = milestoneid
            End If
        End If      

    End Sub

ddlEProjectLevels is dropdownlist in edititemtemplate. When I click edit in 1st row ddlEProjectLevels gets loaded with data from database. But in 2nd row dropdownlist does not contain values. Again in 3rd it gets loaded from db. Means in alternate rows, when I click edit dropdownlist(ddlEProjectLevels) doesn't load values. Can anybody help?

Best Solution

This issue is somewhat confusing compared to other controls and applies to GridView and DetailsView controls. You'll need to check the RowState enumeration using bitwise logic since an item may have a state of Alternate or Edit.

So instead of:

If e.Row.RowState = DataControlRowState.Edit Then


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

For more info check out the DataControlRowState Enumeration page.

Related Question