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

LVL 1
cyimxtckAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jorgesv13Commented:
You need to pass the column names as a parameter. You can pass them comma-separated as you have them now in the Cols function.

Public ReadOnly Property Data(ByVal columns As String) As DataTable
 
        Get
 
            If Session("Data") Is Nothing Then
                Dim table As New DataTable()
                For Each column As String In columns.Split(",")
                    table.Columns.Add(column)
                Next
 
                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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cyimxtckAuthor Commented:
Perfect!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.