Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Gridview is only adding one item. However i want it to add both items

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 553
    Answer it

    My listbox2 items are the items needed to be added as columns. i am using a button to add items from listbox1 to listbox 2. so lets say if listbox1 item x,y coordinate is added to listbox2 item then i need to add column x,y coordinate to the gridview. if listbox1 item latitude, longitude is added to listbox2 then i need to add column latitude, longitude to the gridview. however, if both of the items are added to listbox 2 from listbox 1 then i want to show both columns with rows underneath. the code is below: However, i want to add the listboxes selected on one page and then display it in a grid view on another page. Therefore i created a global variable class which takes the text item from the listbox2.items.text and adds it as explained below:

        Dim dt As New DataTable    
        dt.Clear()
                    For i As Integer = 0 To GlobalVariable.listbox2Count - 1
                        If GlobalVariable.containsListBox2Item = "X,Y Coordinate" Then
                            dt.Columns.Add("X Coordinate")
                            dt.Columns.Add("Y Coordinate")
                        ElseIf GlobalVariable.containsListBox2Item = "Latitude, Longitude" Then
                            dt.Columns.Add("Latitude")
                            dt.Columns.Add("Longitude")
                        End If
                    Next
                    Dim mr As DataRow
                    mr = dt.NewRow
                    If dt.Columns.Contains("X Coordinate") Then
                        mr("X Coordinate") = "100252"
                        mr("Y Coordinate") = "215120"
                    Else
                        mr("Latitude") = "202030"
                        mr("Longitude") = "515123"
                    End If
                    dt.Rows.Add(mr)
                    GridView1.DataSource = dt
                    GridView1.DataBind()

    My mockup for the gridview page is below:

    <asp:GridView ID="GridView1" AutoGenerateColumns="true" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <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>

    My Public Class is Below where I am just taking the count of all listbox2 items and assigning it to a public variable so i can use it on the next page:

    public class GlobalVariable
            public shared listbox2Count = listbox2.items.count
            public shared containsListbox2Item
    End Class

    my code where i assign a text item to a variable object:

    Public Function getListBoxText()
    
            If ListBox2.Text = "X,Y Coordinate" Then
                GlobalVariable.containsListBox2Item = "X,Y Coordinate"
            ElseIf ListBox2.Text = "Latitude, Longitude" Then
                GlobalVariable.containsListBox2Item = "Latitude, Longitude"
    
            Return Nothing
    
    End Function

 2 Answer(s)

  • The gridview will always display only either X,Y co-ordinate or Latitude, Longitude always. This is because of the "Else" clause you have put in the code in line no. 17. You should remove the "Else" and instead, replace line no. Else with code shown below:

         End If
         If dt.Columns.Contains("Latitude") Then                           
    

    With this, the grid view should be able to display both the items X, Y co-ordinate and Latitude, Longitude data. However, if you want the X, Y co-ordinate data and Latitude, Longitude data to be displayed on separate rows, then you need to remove "mr = dt.NewRow" and "dt.Rows.Add(mr)" from outside the If clause, and put it inside both the If clauses: "If dt.Columns.Contains("X Coordinate") Then" and "If dt.Columns.Contains("Latitude") Then".

  • Hi @jigart1992, What i could understand from your back end code is that you are not running any loop while adding rows to dt and only one row is added in a whole process. So i will advice you to run a loop on the collection count for which you want to add data in grid. I hope this answer will give you some solution to your problem.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: