Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

dynamic datatable creation method

Posted on 2008-11-06
2
Medium Priority
?
1,203 Views
Last Modified: 2011-09-20
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

0
Comment
Question by:cyimxtck
2 Comments
 
LVL 8

Accepted Solution

by:
jorgesv13 earned 2000 total points
ID: 22897337
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
 
LVL 1

Author Closing Comment

by:cyimxtck
ID: 31513991
Perfect!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question