Link to home
Start Free TrialLog in
Avatar of cyimxtck
cyimxtckFlag for United States of America

asked on

dynamic datatable creation method

I have this method that creates a datatable for the other methods to use.  This is just a prototype for what I need; the client now wants to have the columns be dynamically determined based on values that they have selected on the front end.

How can I dynamically add the columns?

Everything is selected from a list box and then they click submit for the report output.  I have listed that method below too.\

Basically the property needs to use the function cols instead of the hard coded prototype I built.

Any help is greatly appreciated.

Thanks,

B
Public ReadOnly Property Data() As DataTable
 
        Get
 
            If Session("Data") Is Nothing Then
                Dim table As New DataTable()
                table.Columns.Add("Account_Num")
                table.Columns.Add("Advisor")
                table.Columns.Add("Affiliate_Code")
                table.Columns.Add("Alpha")
 
                Dim i As Integer = 1
                While i <= 20
                    Dim row As DataRow = table.NewRow()
                    row(0) = i
                    row(1) = "Advisor" + i.ToString()
                    row(2) = "Affiliate_Code" + i.ToString()
                    row(3) = "Alpha" + i.ToString()
                    table.Rows.Add(row)
                    System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                End While
 
                Session("Data") = table
 
            End If
 
            Return DirectCast(Session("Data"), DataTable)
 
        End Get
 
    End Property
 
    Protected Function cols() As String
 
        Dim column As New StringBuilder
 
        For i As Integer = 0 To Data.Columns.Count - 1
 
            column.Append(Data.Columns.Item(i))
            column.Append(", ")
 
        Next
 
        Return column.ToString
 
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jorge Sanchez
Jorge Sanchez
Flag of Ecuador image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cyimxtck

ASKER

Perfect!