locked
Gridview loops twice? RRS feed

  • Question

  • User-1767698477 posted

    I have this gridview and I"m using the rowdatabound event to make some changes to the gridview cell values. I have debugged this, and my changes are made on the 1st loop, but I notice that for reason it is looping a 2nd time and undoing what it did the first time. Why is this happening? Why are there 2 rows? There is only one row of data. How do I prevent this 2nd iteration from occuring? Cells 8-11 are bits in the database.

    If e.Row.RowType = DataControlRowType.DataRow Then
                'e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
                Dim Liabtype As String = e.Row.Cells(1).Text
                If Liabtype = "C" Then
                    e.Row.Cells(1).Font.Size = 10
                    e.Row.Cells(1).Text = "Collections, Judgments, & Liens"
                End If
                If Liabtype = "H" Then
                    e.Row.Cells(1).Text = "Home Equity Line of Credit"
                End If
                If Liabtype = "I" Then
                    e.Row.Cells(1).Text = "Installment"
                End If
                If Liabtype = "L" Then
                    e.Row.Cells(1).Text = "Lease"
                End If
                If Liabtype = "M" Then
                    e.Row.Cells(1).Text = "Mortgage"
                End If
                If Liabtype = "O" Then
                    e.Row.Cells(1).Text = "Open 30-day charge Account"
                End If
                If Liabtype = "OT" Then
                    e.Row.Cells(1).Text = "Other"
                End If
                If Liabtype = "R" Then
                    e.Row.Cells(1).Text = "Revolving"
                End If
                If Liabtype = "T" Then
                    e.Row.Cells(1).Text = "Taxes"
                End If
                If Liabtype = "TL" Then
                    e.Row.Cells(1).Text = "Tax Lien"
                End If
    
                Dim var As String = e.Row.Cells(8).Text
                If var = "True" Then
                    e.Row.Cells(8).Text = "Y"
                Else e.Row.Cells(8).Text = ""
                End If
                var = e.Row.Cells(9).Text
                If var = "True" Then
                    e.Row.Cells(9).Text = "Y"
                Else e.Row.Cells(9).Text = ""
                End If
                var = e.Row.Cells(10).Text
                If var = "True" Then
                    e.Row.Cells(10).Text = "Y"
                Else e.Row.Cells(10).Text = ""
                End If
                var = e.Row.Cells(11).Text
                If var = "True" Then
                    e.Row.Cells(11).Text = "Y"
                Else e.Row.Cells(11).Text = ""
                End If
    
            End If

    Wednesday, May 5, 2021 3:12 AM

Answers

  • User-939850651 posted

    Hi sking,

    sking

    Here is my code. I'm loading the gridview from the sqladapter on the aspx page. I'm not putting all the columns into the gridview though. When I open the row editor with the modal popup, there are fields which I didn't get from the sqldatasource. I'm getting the missing columns to edit the row data from calling a table adapter which produces 1 row. There is only 1 row in the table right now.

    I think your problem should be related to the RowDataBound event instead of this button event, and it seems that the GridView datasource has not been rebind in the button event.

    What you need to confirm is whether you have bound the RowDataBound twice (aspx side and Code behind), and you need to confirm whether the GridView datasource is rebind in other postback events. 

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 6, 2021 9:07 AM

All replies

  • User-939850651 posted

    Hi sking,

    Did you double bind the event? It means this event is added to the GridView control in the aspx, and the handle is added to CodeBehind again.

    For example:

    <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false" OnRowDataBound="GV1_RowDataBound" >
    ......
    </asp:GridView> 
    Protected Sub GV1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GV1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                ......
            End If
        End Sub

    If I misunderstood anything, please provide a minimal example to reproduce the problem, it will help to find the cause of the problem.

    Best regards,

    Xudong Peng

    Wednesday, May 5, 2021 7:43 AM
  • User-1767698477 posted

    Hi,

    Here is my code. I'm loading the gridview from the sqladapter on the aspx page. I'm not putting all the columns into the gridview though. When I open the row editor with the modal popup, there are fields which I didn't get from the sqldatasource. I'm getting the missing columns to edit the row data from calling a table adapter which produces 1 row. There is only 1 row in the table right now.

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MortgageAppsConnectionString %>" SelectCommand="SELECT Type, Othertype, Name, Street, City, State, Zip,  Acctnum, Mopmt, Molefttopay, Balance, Paidatclosing, PaidPTC, Resub, Omit, REOnum FROM Liabilities WHERE (ApplicantID = @ApplicantID)">

     <div style="overflow-x: auto; height: 150px; width: 700px">
                            <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellPadding="3" Caption="Liabilities" CaptionAlign="Left" HorizontalAlign="Center" OnRowDataBound="Gridview1_rowdatabound" RowStyle-Wrap="False" HeaderStyle-Wrap="False">
                                <PagerStyle Wrap="True" />
                                <RowStyle HorizontalAlign="Center" />
                                <Columns>
                                    <asp:TemplateField ItemStyle-Width="30px" HeaderText="">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="imgbtn" ImageUrl="~/templates/images/pencil.gif" runat="server" Width="25" Height="25" OnClick="imgbtn_Click" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                                    <asp:BoundField DataField="Othertype" HeaderText="Other type" SortExpression="Othertype" />
                                    <asp:BoundField DataField="Name" HeaderText="Company" SortExpression="Name" />
                                    <asp:BoundField DataField="Acctnum" HeaderText="Acct #" SortExpression="Acctnum" />
                                    <asp:BoundField DataField="Mopmt" HeaderText="Payment" SortExpression="Mopmt" />
                                    <asp:BoundField DataField="Balance" HeaderText="Balance" SortExpression="Balance" />
                                    <asp:BoundField DataField="Molefttopay" HeaderText="Months left" SortExpression="Molefttopay" />
                                    <asp:BoundField DataField="Paidatclosing" HeaderText="Paid @ close" SortExpression="Paidatclosing" />
                                    <asp:BoundField DataField="PaidPTC" HeaderText="Paid PTC" SortExpression="PaidPTC" />
                                    <asp:BoundField DataField="Resub" HeaderText="Resubordinate" SortExpression="Resub" />
                                    <asp:BoundField DataField="Omit" HeaderText="Omit" SortExpression="Omit" />
                                    <asp:BoundField DataField="REOnum" HeaderText="REO #" SortExpression="REOnum" />
                                </Columns>
                            </asp:GridView>
                        </div>
                        <center><asp:Label ID="lblresult" runat="server" forecolor="Green"/></center>
                        <asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
                        <asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlpopup" CancelControlID="btnCancel" BackgroundCssClass="modalBackground">
                        </asp:ModalPopupExtender>
                        <asp:Panel ID="pnlpopup" runat="server" BackColor="White" Height="260px" Width="760px" Style="display: none">
                            <table width="100%" style="border: Solid 3px #000000; width: 100%; height: 100%" cellpadding="0" cellspacing="0">
     Protected Sub imgbtn_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
            Dim tblliabilitiesadapter As New DataSet1TableAdapters.LiabilitiesTableAdapter
            Dim tblliabilities As DataSet1.LiabilitiesDataTable
            tblliabilities = tblliabilitiesadapter.GetData(Session("ApplicantID"))
            For Each tblliabilitiesrow As DataSet1.LiabilitiesRow In tblliabilities
                Dim btndetails As ImageButton = TryCast(sender, ImageButton)
                Dim gvrow As GridViewRow = DirectCast(btndetails.NamingContainer, GridViewRow)
                'lblID.Text = GridView1.DataKeys(gvrow.RowIndex).Value.ToString()
                ddliabtype.SelectedValue = gvrow.Cells(1).Text
                txtliabname.Text = gvrow.Cells(3).Text
                txtliabaccountnum.Text = gvrow.Cells(4).Text
                txtliabstreet.Text = tblliabilitiesrow.Street
                ddunitdesignator.SelectedValue = tblliabilitiesrow.Unitdesignator
                txtliabunitnum.Text = tblliabilitiesrow.Unitnum
                txtliabcity.Text = tblliabilitiesrow.City
                txtliabstate.Text = tblliabilitiesrow.State
                txtliabzip.Text = tblliabilitiesrow.Zip
                ddliabmtgtype.SelectedValue = tblliabilitiesrow.Mortgagetype
                txtliabcreditlimit.Text = tblliabilitiesrow.CreditLimit
                cbliabtaxes.Checked = tblliabilitiesrow.Pmtincltax
                cbliabins.Checked = tblliabilitiesrow.Pmtinclins
                txtliabmopmt.Text = gvrow.Cells(5).Text
                txtliabmoleft.Text = gvrow.Cells(6).Text
                txtliabbal.Text = gvrow.Cells(7).Text
                If tblliabilitiesrow.Paceloan = False Then
                    cbliabpaceloan.Checked = False
                Else
                    cbliabpaceloan.Checked = True
                End If
                If tblliabilitiesrow.Paidatclosing = False Then
                    cbliabpoff.Checked = False
                Else
                    cbliabpoff.Checked = True
                End If
    
                If tblliabilitiesrow.PaidPTC = False Then
                    cbliabpdbefore.Checked = False
                Else
                    cbliabpdbefore.Checked = True
                End If
                If tblliabilitiesrow.Resub = False Then
                    cbliabresubord.Checked = False
                Else
                    cbliabresubord.Checked = True
                End If
                If tblliabilitiesrow.Omit = False Then
                    cbliabomit.Checked = False
                Else
                    cbliabomit.Checked = True
                End If
            Next
            Me.ModalPopupExtender1.Show()
        End Sub

    Wednesday, May 5, 2021 5:16 PM
  • User-939850651 posted

    Hi sking,

    sking

    Here is my code. I'm loading the gridview from the sqladapter on the aspx page. I'm not putting all the columns into the gridview though. When I open the row editor with the modal popup, there are fields which I didn't get from the sqldatasource. I'm getting the missing columns to edit the row data from calling a table adapter which produces 1 row. There is only 1 row in the table right now.

    I think your problem should be related to the RowDataBound event instead of this button event, and it seems that the GridView datasource has not been rebind in the button event.

    What you need to confirm is whether you have bound the RowDataBound twice (aspx side and Code behind), and you need to confirm whether the GridView datasource is rebind in other postback events. 

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 6, 2021 9:07 AM
  • User-1767698477 posted

    I switched my code slightly making separate var variables.  It is working for me now although it still loops around twice.

       'paid at close
                Dim var1 As String = e.Row.Cells(8).Text
                If var1 = "True" Then
                    e.Row.Cells(8).Text = "Y"
                ElseIf e.Row.Cells(8).Text = "False" Then
                    e.Row.Cells(8).Text = ""
                End If
                'paid ptc
                Dim var2 = e.Row.Cells(9).Text
                If var2 = "True" Then
                    e.Row.Cells(9).Text = "Y"
                ElseIf e.Row.Cells(9).Text = "False" Then
                    e.Row.Cells(9).Text = ""
                End If
                'resubordinate
                Dim var3 = e.Row.Cells(10).Text
                If var3 = "True" Then
                    e.Row.Cells(10).Text = "Y"
                ElseIf e.Row.Cells(10).Text = "False" Then
                    e.Row.Cells(10).Text = ""
                End If
                'omit
                Dim var4 = e.Row.Cells(11).Text
                If var4 = "True" Then
                    e.Row.Cells(11).Text = "Y"
                ElseIf e.Row.Cells(11).Text = "False" Then
                    e.Row.Cells(11).Text = ""
                End If

    Friday, May 7, 2021 2:03 AM