• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1728
  • Last Modified:

Binding a DropDownList control within a GridView EditItemTemplate

Hi Experts - I have a grid view that uses TemplateFields for columns.  I have a dropdownlist in a EditItemTemplate block that I'm trying to populate from the code behind, but I'm not having any luck.

<asp:TemplateField HeaderText="BinLoc" SortExpression="Bin">
    <ItemStyle VerticalAlign="Top" />
    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="generaltext_A2A">
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("BinLoc") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>

Open in new window


I've try using the following code but I'm getting the old "Object reference not set to an instance of an object." message.

    Protected Sub ItemsGrid_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ItemsGrid.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)

            '  Bind the Bin Loc to the drop down list
            ' -------------------------------------------------------------------------------
            Dim MyBLL As New PickPackBLL()
            Dim dt As DataTable = MyBLL.GetBinLocs()

            ddl.DataSource = dt
            ddl.DataTextField = "BinLoc"
            ddl.DataValueField = "BinLoc"

            '  Set the Selected Value of the drop down list.
            ' -------------------------------------------------------------------------------
            Dim SelectedBinLoc As String = "Unassigned"

            Dim pickTicketLogic As New PickPackItemsBLL()
            Dim dtpick As DataTable = pickTicketLogic.GetPickTicketItems(hfPickTicketID.Value)

            If dtpick.Rows.Count > 0 Then
                Dim row As DataRow = dtpick.Rows(0)
                SelectedBinLoc = row("BinLoc")
            End If

            Try
                ddl.SelectedValue = SelectedBinLoc
            Catch ex As Exception
                ddl.SelectedValue = "Unassigned"
            End Try

        End If
    End Sub

Open in new window


The error is pointing to this...

ddl.DataSource = dt

Open in new window


When I debug the code it states that the variable "ddl" is "nothing"?

Could someone tell me what I'm doing wrong and how I can fix this.

Thank you!
0
cdemott33
Asked:
cdemott33
  • 3
  • 2
1 Solution
 
informaniacCommented:
If e.Row.RowType = DataControlRowType.DataRow Then

instead of DataControlRowType.Header
0
 
cdemott33Author Commented:
Unfortunately its still the same error:  

    Protected Sub ItemsGrid_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ItemsGrid.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim ddl As DropDownList = DirectCast(e.Row.FindControl("DropDownList1"), DropDownList)

            '  Bind the Bin Loc to the drop down list
            ' -------------------------------------------------------------------------------
            Dim MyBLL As New PickPackBLL()
            Dim dt As DataTable = MyBLL.GetBinLocs()

            ddl.DataSource = dt
            ddl.DataTextField = "BinLoc"
            ddl.DataValueField = "BinLoc"
            ddl.DataBind()

            '  Set the Selected Value of the drop down list.
            ' -------------------------------------------------------------------------------
            Dim SelectedBinLoc As String
            Dim defaultSelectedIndex As String = "Unassigned"

            Dim pickTicketLogic As New PickPackItemsBLL()
            Dim dtpick As DataTable = pickTicketLogic.GetPickTicketItems(hfPickTicketID.Value)

            If dtpick.Rows.Count > 0 Then
                Dim row As DataRow = dtpick.Rows(0)
                SelectedBinLoc = row("BinLoc")
            End If

            Try
                ddl.SelectedValue = SelectedBinLoc
            Catch ex As Exception
                ddl.SelectedValue = defaultSelectedIndex
            End Try

        End If
    End Sub

Open in new window


Could it have something to do with the EditTemplate?
0
 
informaniacCommented:
Hmm. Yes.

You need to add the check if the GridView is EditMode or not

if(Gv.EditIndex != -1)
0
 
informaniacCommented:
Sorry should be

if(Gv.EditIndex = e.Row.RowIndex)
0
 
cdemott33Author Commented:
Thank you. That did the trick!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now