Search code examples
asp.netvb.netgridviewdrop-down-menurowdatabound

Dropdown list inside gridview binding for alternate rows on Rowdatabound


I have DropdownList inside gridviewin EditTemmplate which I am binding on RowDatabound. But DropdlownList is binding for alternate rows only. For Example, For first row its binding, not for 2nd, for 3rd it is, and so on. Below is my code:

 Protected Sub grvTdsMaster_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grvTdsMaster.RowDataBound
    Try
        lblErrorMsg.Visible = False
        lblErrorMsg.InnerText = ""
        If (e.Row.RowType = DataControlRowType.DataRow) Then

            If ((e.Row.RowState = DataControlRowState.Edit)) Then
                Dim ddlSection As DropDownList = e.Row.FindControl("ddlSection")
                Dim objTds As New TdsMasterDL
                Dim dt As New DataTable
                dt = objTds.GetTdsSectionName()
                ddlSection.DataSource = dt
                ddlSection.DataValueField = "TDS_Section_Id"
                ddlSection.DataTextField = "TDS_Section_Name"
                ddlSection.DataBind()

            End If
        End If
    Catch ex As Exception
        'Additional info that could be useful for debugging error
        Dim sb As New StringBuilder
        sb.Append("User=" & Session("username"))

        ExceptionHandler(ex, sb.ToString)
    End Try
End Sub

Solution

  • I think the RowState is alternate by default. Check DataControlRowState Enum on MSDN. This should work for you:-

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