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)