Him_Jalpert
Him_Jalpert

Reputation: 2516

Inconsistency with dropdowns displaying values in gridview

I'm having a problem with my dropdown lists in my child gridview displaying values. For whatever reason the first dropdown in the row works fine and displays the values from the database, the other ones don't display anything.

Below is the code for my gridviews:

    <asp:GridView ID="GVAccounts" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333" 
        GridLines="Horizontal" 
        style="position: relative; margin-top: 10px">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="branch" HeaderText="Branch" 
                SortExpression="branch" />
            <asp:BoundField DataField="no" HeaderText="Account" 
                SortExpression="account" />
            <asp:TemplateField HeaderText="Name" SortExpression="name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="LabelName" runat="server" Text='<%# Eval("name") + " " + Eval("surname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
            <asp:BoundField DataField="sub" HeaderText="Sub" SortExpression="sub" />
            <asp:BoundField DataField="TotalAmount" HeaderText="Hold" 
                SortExpression="TotalAmount" />
            <asp:BoundField DataField="loc" HeaderText="LOC" DataFormatString="{0:C}" SortExpression="loc" />

            <asp:BoundField DataField="locstatus" HeaderText="LOC status" 
                SortExpression="locstatus" />

            <asp:BoundField DataField="HoldCalc" HeaderText="OD/EX Amt" SortExpression="HoldCalc" />
            <asp:BoundField DataField="odtimes" HeaderText="#OD" SortExpression="odtimes" />

            <asp:TemplateField>
                <ItemTemplate>
                    <tr>

                    <td colspan="100%">
                        <div id="gridChild" style="display: inline; position: relative; left: 15px; overflow: auto">

                        <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" 
                                BorderStyle="Double"  BorderColor="#5D7B9D" Width="80%">

                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />

                            <RowStyle BackColor="#E1E1E1" />

                            <AlternatingRowStyle BackColor="White" />

                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="true" ForeColor="White" />

                            <Columns>

                                <asp:TemplateField HeaderText="Select">
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true"
                                            oncheckedchanged="chkSelect_CheckedChanged" EnableViewState="true" />
                                    </ItemTemplate>  
                                </asp:TemplateField>

                                <asp:BoundField DataField="effective" HeaderText="Effective" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="desc_" HeaderText="Desc" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="code" HeaderText="TC" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="amount" HeaderText="Amount" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="tracer" HeaderText="Cheq #" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="empl" HeaderText="Empl" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="balance" HeaderText="OD/EXT Amt" 
                                    HeaderStyle-HorizontalAlign="Left" >
                                <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:TemplateField HeaderText="Note">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="DropDownNote" runat="server" 
                                            onselectedindexchanged="DropDownNote_SelectedIndexChanged" 
                                            AutoPostBack="True">
                                            <asp:ListItem></asp:ListItem>
                                            <asp:ListItem>MWC</asp:ListItem>
                                            <asp:ListItem>CBM</asp:ListItem>
                                            <asp:ListItem>Return</asp:ListItem>
                                            <asp:ListItem>TSF</asp:ListItem>
                                            <asp:ListItem>OK NO S/C</asp:ListItem>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Note1">
                                    <ItemTemplate>
                                            <asp:DropDownList ID="ddNote2" runat="server" 
                                                onselectedindexchanged="ddNote2_SelectedIndexChanged" AutoPostBack="true">
                                                <asp:ListItem Selected="True">NSF</asp:ListItem>
                                                <asp:ListItem>Funds Not Clear</asp:ListItem>
                                                <asp:ListItem>Post Dated</asp:ListItem>
                                                <asp:ListItem>Stale Dated</asp:ListItem>
                                                <asp:ListItem>Stop Payment</asp:ListItem>
                                                <asp:ListItem>Encoding Incorrect</asp:ListItem>
                                            </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Branch">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddBranch" runat="server" DataSourceID="BranchDataSource" 
                                            DataTextField="branch" DataValueField="branch" AutoPostBack="true">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="BranchDataSource" runat="server" 
                                            ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>" 
                                            SelectCommand="SELECT [branch] FROM [branch]"></asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Account">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TextNo" runat="server" ></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Type">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddType" runat="server" DataSourceID="typeSource" 
                                            DataTextField="Type" DataValueField="Type" AutoPostBack="true">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="typeSource" runat="server" 
                                            ConnectionString="<%$ ConnectionStrings:ThirdPartyDataConnectionString %>" 
                                            SelectCommand="SELECT [Type] FROM [DMDType]"></asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Sub">
                                    <ItemTemplate><asp:TextBox ID="TextSub" Width="25px" runat="server"></asp:TextBox></ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

                        </div> 
                    </td>

                    </tr>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="GVFixedHeader" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

Below is the method that is having the issue with displaying:

Private Function CheckForSubmit() As Boolean
    Try
        conn = New SqlConnection(connectionString)

        Dim cmdNote As New SqlCommand("select note, note1, noteBranch, noteAccountNo, noteType, noteSub, id from DmdOD where id = @id order by effective, time_, seqno_dmddmhi_dmhi", conn)
        cmd = New SqlCommand("select submitted, id, effective FROM DmdOD where no = @no and entered = '" & ddDate.SelectedItem.Text & "' order by effective, time_, seqno_dmddmhi_dmhi", conn)


        Dim objDBId As Object = 0
        Dim id = 0
        Dim objDate As Date
        Dim dateStr As String = ""

        If ddDate.SelectedIndex <> -1 Then
            cmd = New SqlCommand("select submitted, id, entered FROM DmdOD where no = @no and entered = '" & ddDate.SelectedItem.Text & "' order by effective, time_, seqno_dmddmhi_dmhi", conn)
            '    cmd.Parameters.Add(New SqlParameter("effective", ddDate.SelectedItem.Text))
        Else
            cmd = New SqlCommand("select submitted, id, entered FROM DmdOD where no = @no order by effective, time_, seqno_dmddmhi_dmhi", conn)
        End If
        cmd.CommandType = CommandType.Text
        Dim i = 0

        For Each row As GridViewRow In GVAccounts.Rows
            cmd.Parameters.Add(New SqlParameter("@no", row.Cells(1).Text))

            Dim gv As GridView = DirectCast(row.FindControl("gvChildGrid"), GridView)


            conn.Open()

            Dim idReader As SqlClient.SqlDataReader = cmd.ExecuteReader()
            'get ids in order
            While (idReader.Read())
                If Not objId.Contains(idReader.GetValue(1)) Then
                    objId.Add(idReader.GetValue(1))
                End If
            End While
            conn.Close()

            conn.Open()
            Dim reader As SqlClient.SqlDataReader = cmd.ExecuteReader()

            While (reader.Read())
                objSubmit = reader.GetValue(0)
                objDBId = reader.GetValue(1)
                objDate = reader.GetValue(2)
                dateStr = objDate.ToShortDateString
                If dateStr = ddDate.SelectedItem.Text Then
                    Exit While
                End If
            End While

            conn.Close()
            If objSubmit = True And dateStr = ddDate.SelectedItem.Text Then

                For Each r As GridViewRow In gv.Rows

                    cmdNote.Parameters.Add(New SqlParameter("@id", objId(i)))

                    Dim ddNote As Control = r.FindControl("DropDownNote")
                    Dim ddl As DropDownList = DirectCast(ddNote, DropDownList)

                    Dim ddNote2 As Control = r.FindControl("ddNote2")
                    Dim ddlNote As DropDownList = DirectCast(ddNote2, DropDownList)
                    ddlNote.Items.Insert(0, New ListItem(String.Empty, String.Empty))

                    Dim ddBranch As Control = r.FindControl("ddBranch")
                    Dim ddBranchBox As DropDownList = DirectCast(ddBranch, DropDownList)
                    ddBranchBox.Items.Insert(0, New ListItem(String.Empty, String.Empty))

                    Dim txtAccount As Control = r.FindControl("TextNo")
                    Dim txtAccountBox As TextBox = DirectCast(txtAccount, TextBox)

                    Dim ddType As Control = r.FindControl("ddType")
                    Dim ddTypeBox As DropDownList = DirectCast(ddType, DropDownList)

                    Dim txtSub As Control = r.FindControl("TextSub")
                    Dim txtSubBox As TextBox = DirectCast(txtSub, TextBox)

                    r.Cells(0).Enabled = False
                    r.Cells(8).Enabled = False
                    r.Cells(9).Enabled = False
                    r.Cells(10).Enabled = False
                    r.Cells(11).Enabled = False
                    r.Cells(12).Enabled = False
                    r.Cells(13).Enabled = False

                    'Display fields
                    gv.Columns(8).Visible = True
                    gv.Columns(9).Visible = True
                    gv.Columns(10).Visible = True
                    gv.Columns(11).Visible = True
                    gv.Columns(12).Visible = True
                    gv.Columns(13).Visible = True

                    ddBranchBox.Visible = True
                    txtAccount.Visible = True
                    ddTypeBox.Visible = True
                    txtSub.Visible = True

                    'Get Data for fields
                    conn.Open()
                    cmdNote.ExecuteNonQuery()
                    Dim readNotes As SqlClient.SqlDataReader = cmdNote.ExecuteReader()
                    readNotes.Read()
                    'Note
                    If IsDBNull(readNotes.GetValue(0)) Then
                        ddl.SelectedItem.Text = ""
                    Else
                        ddl.SelectedItem.Text = readNotes.GetValue(0)
                    End If
                    'Note1

                    If IsDBNull(readNotes.GetValue(1)) Then
                        'GVAccounts.Columns(12).Visible = False
                        ddlNote.Visible = False
                        ddlNote.SelectedItem.Text = ""
                    Else
                        'ddlNote.SelectedItem.Text = readNotes.GetValue(1)
                        ddlNote.SelectedItem.Selected = False
                        ddlNote.Items.FindByText(readNotes.GetValue(1)).Selected = True
                    End If
                    'Branch
                    If IsDBNull(readNotes.GetValue(2)) Then
                        ddBranchBox.Visible = False
                        ddBranchBox.SelectedItem.Text = ""
                    Else
                        ddBranchBox.SelectedItem.Text = readNotes.GetValue(2)
                    End If
                    'Account
                    If IsDBNull(readNotes.GetValue(3)) Then
                        txtAccountBox.Visible = False
                        txtAccountBox.Text = ""
                    Else
                        txtAccountBox.Text = readNotes.GetValue(3)
                    End If
                    'Type
                    If IsDBNull(readNotes.GetValue(4)) Then
                        ddTypeBox.Visible = False
                        ddTypeBox.SelectedItem.Text = ""
                    Else
                        ddTypeBox.SelectedItem.Text = readNotes.GetValue(4)
                    End If
                    'Sub
                    If IsDBNull(readNotes.GetValue(5)) Then
                        txtSubBox.Visible = False
                        txtSubBox.Text = ""
                    Else
                        txtSubBox.Text = readNotes.GetValue(5)
                    End If
                    conn.Close()
                    i += 1
                    cmdNote.Parameters.Clear()
                Next
                ButtonSubmit.Enabled = False
                ButtonSave.Enabled = False
                ButtonSaveTop.Enabled = False
            End If
            i = 0
            'cmd.Parameters.Clear()
            'cmdNote.Parameters.Clear()
            objSubmit = False
            dateStr = ""
            cmd.Parameters.RemoveAt(0)
            cmdNote.Parameters.Clear()
            objId.Clear()
        Next

        If objSubmit = True Then
            Return objSubmit
        End If


    Catch ex As Exception
        LblErr.ForeColor = Drawing.Color.DarkRed
        LblErr.Text = "CheckForSubmit - " & ex.Message
    Finally
        conn.Dispose()
    End Try
    Return Nothing
End Function

Edit: I should also note that when I step through the code I see that the dropdowns do in fact have values, yet nothing is being displayed for them on the gridview, I'm stumped.

Upvotes: 0

Views: 110

Answers (1)

Robert Anderson
Robert Anderson

Reputation: 1256

Updated answer

I would compare drop down lists "note" and "note1"

e.g. note works but note1 doesn't.

The difference I can see between the 2 is that note has an empty list item. Try adding an empty list item to note 1.

Failing that, copy the working code from note and gradually replace it with the values from Note1, checking if/when it fails.

Upvotes: 1

Related Questions