DallasWebCenter
asked on
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(my Column)
... and then later, in a for/next loop I have:
Row = myDataTable.Rows(RowNum)
Row.Item("myColumn") = NumX
Thanks for help with my question!
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(my
... and then later, in a for/next loop I have:
Row = myDataTable.Rows(RowNum)
Row.Item("myColumn") = NumX
Thanks for help with my question!
Hi DallasWebCenter, I don't see anything wrong with it, what doesn't work what error do you get.
how did you define Row?
how did you define Row?
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("Tot al",Type.G etType("Sy stem.Doubl e"))
workTable.Columns.Add("Sal esTax", Type.GetType("System.Doubl e"), "Total * 0.086")
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("Tot
workTable.Columns.Add("Sal
ASKER
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.ArgumentNullExcepti on: 'column' argument cannot be null. Parameter name: column
Line 73: TableStats.Columns.Add(myC olumn)
'column' argument cannot be null. Parameter name: column
Exception Details: System.ArgumentNullExcepti
Line 73: TableStats.Columns.Add(myC
ASKER
TableStats is the correct name for myDataTable.
how is mycolumn defined
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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.ICon tainer
'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.DataG rid
Friend WithEvents Button1 As System.Windows.Forms.Butto n
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataG rid
Me.Button1 = New System.Windows.Forms.Butto n
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).Begi nInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol or = System.Drawing.SystemColor s.ControlT ext
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.DataGri d1)
Me.Name = "DataTableCreation"
Me.Text = "DataTableCreation"
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).EndI nit()
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("C OL4", System.Type.GetType("Syste m.Double") )
For Each Row In myDataTable.Rows
Row.Item("COL4") = "3"
Next Row
End Sub
Private Sub DataTableCreation_Load(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myDataTable = New DataTable("Group")
myColumn = myDataTable.Columns.Add("C OL1", System.Type.GetType("Syste m.String") )
myColumn = myDataTable.Columns.Add("C OL2", System.Type.GetType("Syste m.Double") )
myColumn = myDataTable.Columns.Add("C OL3", System.Type.GetType("Syste m.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.
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.ICon
'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.DataG
Friend WithEvents Button1 As System.Windows.Forms.Butto
<System.Diagnostics.Debugg
Me.DataGrid1 = New System.Windows.Forms.DataG
Me.Button1 = New System.Windows.Forms.Butto
CType(Me.DataGrid1, System.ComponentModel.ISup
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol
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.DataGri
Me.Name = "DataTableCreation"
Me.Text = "DataTableCreation"
CType(Me.DataGrid1, System.ComponentModel.ISup
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("C
For Each Row In myDataTable.Rows
Row.Item("COL4") = "3"
Next Row
End Sub
Private Sub DataTableCreation_Load(ByV
myDataTable = New DataTable("Group")
myColumn = myDataTable.Columns.Add("C
myColumn = myDataTable.Columns.Add("C
myColumn = myDataTable.Columns.Add("C
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.
ASKER
Chaosian was right. I needed to declare my datatype first.
Thanks for all your help.
Thanks for all your help.
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).I