Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

VB
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.NET
<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:

VB
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:

VB
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



What I have tried:

i tried googling and debugging butting break points but only one item adds.
Posted
Updated 5-May-16 8:57am

1 solution

That is because ypu have added codes in IF and ELSE IF. So, one will excute at a time. Remove ELSE IF and add it in a IF.
 
Share this answer
 
Comments
JT1992 5-May-16 15:10pm    
do i need to add anything in an array? I tried it didn't work
Please debug and se if the lines which add the columns are getting executed or not.
JT1992 9-May-16 15:06pm    
thanks worked. please upvote the question for future guests
Sure. Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900