Would like to add column to existing Datatable, then add items to it one at a time.

I'm quite sure this isn't the correct way to do this, but I'm also equally sure I have little to no idea what I'm talking about.
Either way, here goes.

I have an existing Datatable which contains data similar to:

COL1        COL2        COL3
Name1     Num1       Num2
Name2     Num1       Num2
Name3     Num1       Num2

I would like to add another column, and then add values to that column.
For example, after one pass in a for/next loop, the datatable would be:

COL1        COL2        COL3        COL4
Name1     Num1       Num2       Num3
Name2     Num1       Num2
Name3     Num1       Num2

After the second pass in the loop, the datatable would be:

COL1        COL2        COL3        COL4
Name1     Num1       Num2       Num3
Name2     Num1       Num2       Num3
Name3     Num1       Num2

I hope that makes sense.
Can I add a column to a datatable and then add items one at a time?

Here's what I have (which doesn't work of course):

myDataTable.Columns.Add(myColumn)

... and then later, in a for/next loop I have:

Row = myDataTable.Rows(RowNum)
Row.Item("myColumn") = NumX

Thanks for help with my question!
DallasWebCenterAsked:
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.

LizelleFanCommented:
hi

I hope i'm understanding you problem. You can add extra columns after your first pass and then just go and update that column for the first or second or whatever row.
So if you want to update COL4 for the first row during the second pass you would say
myDataTable.Rows.Item(0).Item(3) = "Num3"
0
RonaldBiemansCommented:
Hi DallasWebCenter, I don't see anything wrong with it, what doesn't work what error do you get.
how did you define Row?
0
Jeff CertainCommented:
Is myColumn created correctly?

Also, you can loop a little more elegantly:
dim row as datarow
for each row in myDataTable.Rows
  Row.Item("myColumn") = NumX
Next row

If the new column is calculated based on other columns, you can also do something like this to create the new column without haveing to loop through the rows at all:
workTable.Columns.Add("Total",Type.GetType("System.Double"))
workTable.Columns.Add("SalesTax", Type.GetType("System.Double"), "Total * 0.086")
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

DallasWebCenterAuthor Commented:
Currently, when run (without implementing any of the changes suggested here), I get the following error:

'column' argument cannot be null. Parameter name: column

Exception Details: System.ArgumentNullException: 'column' argument cannot be null. Parameter name: column
Line 73:         TableStats.Columns.Add(myColumn)
0
DallasWebCenterAuthor Commented:
TableStats is the correct name for myDataTable.
0
RonaldBiemansCommented:
how is mycolumn defined
0
Jeff CertainCommented:
Instead of this
myDataTable.Columns.Add(myColumn)
you need to have something that looks like this:

Dim myColumn  as dataColumn
TableStats.Columns.Add("Total",Type.GetType("System.Double"))
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
Jeff CertainCommented:
Sorry... you don't need to define myColumn if you use the method above...otherwise you have to declare it, define it and add it...

0
mani_saiCommented:
Here is the sample Code:



Public Class DataTableCreation
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.Button1 = New System.Windows.Forms.Button
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(32, 48)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(336, 144)
        Me.DataGrid1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(136, 216)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'DataTableCreation
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "DataTableCreation"
        Me.Text = "DataTableCreation"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim myDataTable As DataTable
    Dim myColumn As DataColumn
    Dim Row As DataRow

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        myColumn = myDataTable.Columns.Add("COL4", System.Type.GetType("System.Double"))

        For Each Row In myDataTable.Rows
            Row.Item("COL4") = "3"
        Next Row

    End Sub

    Private Sub DataTableCreation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        myDataTable = New DataTable("Group")

        myColumn = myDataTable.Columns.Add("COL1", System.Type.GetType("System.String"))
        myColumn = myDataTable.Columns.Add("COL2", System.Type.GetType("System.Double"))
        myColumn = myDataTable.Columns.Add("COL3", System.Type.GetType("System.Double"))



        myDataTable.Rows.Add(New Object() {"Name1", "1", "2"})
        myDataTable.Rows.Add(New Object() {"Name2", "1", "2"})
        myDataTable.Rows.Add(New Object() {"Name3", "1", "2"})

        DataGrid1.DataSource = myDataTable

    End Sub
End Class


Good Luck.
0
DallasWebCenterAuthor Commented:
Chaosian was right. I needed to declare my datatype first.
Thanks for all your help.
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.