Dynamically Generating a Data-Bound Form from SQL View

slybitz

Member
Joined
Aug 16, 2005
Messages
9
Programming Experience
Beginner
So I have a Windows application form and on that form I'm trying to loop through the columns collection of a DataTable that I populated with data, and for each column, I want to create a label and text box. So below is what I came up with but it only outputs rows and not columns. How do I get this bit of code, or something similar, to output seperated columns from the DataTable and not single rows like it's doing?

VB.NET:
For I = 0 To table2.Columns.Count - 1
    label = New Label()
    label.Location = New Point(10, I * 22 + 10)
    label.AutoSize = True
    label.Text = table2.Columns(I).ColumnName + ":"
    Controls.Add(label)
    textBox = New TextBox
    textBox.Location = New Point(100, I * 22 + 10)
    textBox.Width = 200
    textBox.DataBindings.Add("Text", _
       table2, table2.Columns(I).ColumnName)
    Controls.Add(textBox)
Next

...and here's how I populated my DataTable:

VB.NET:
        Dim table2 As DataTable = Nothing

        Dim connectionString2 As String = _
           "[URL="http://www.tek-tips.com/viewthread.cfm?qid=1204533&page=1#"]integrated security[/URL]=SSPI;[URL="http://www.tek-tips.com/viewthread.cfm?qid=1204533&page=1#"]data source[/URL]=LONGS;initial catalog=AFD;Application Name='PAP2'"

        Using connection2 As SqlConnection = New SqlConnection(connectionString)

            connection2.Open()

            Dim command2 As SqlCommand = _
               New SqlCommand("SELECT GLAcctNo, [description], sum(PostAmt) PostAmt, sum(PostAmtHC) PostAmtHC FROM vPAP_BatchApprPop WHERE BatchID = '" & Me.lblBatch.Text & "' GROUP BY [description], GLAcctNo", connection2)

            Dim adapter2 As SqlDataAdapter = New SqlDataAdapter(command2)

            table2 = New DataTable()

            adapter2.Fill(table2)

        End Using
 
Sorry for the double post but what I'm trying to do is make it look like this:

VB.NET:
column#1      column#2     column#2
--------      --------     --------
c#1recordA    c#2recordA   c#3recordA
c#1recordB    c#2recordB   c#3recordB
c#1recordC    c#2recordC   c#3recordC
.
.
.

...I'm not sure how to do this. Thanks for the help.
 
Back
Top