Adding rows to a data table: Rows do not show up

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello:

I defined a datatable with two columns:

dtTable = New DataTable("Names")

' Add three column objects to the table.
idColumn = New DataColumn()
idColumn.DataType = System.Type.GetType("System.String")
idColumn.ColumnName = "AER Number"
idColumn.MaxLength = 20
dtTable.Columns.Add(idColumn)

idColumn = New DataColumn()
idColumn.DataType = System.Type.GetType("System.String")
idColumn.ColumnName = "Case Version"
idColumn.MaxLength = 3
dtTable.Columns.Add(idColumn)

I am trying to insert rows into the table:

Dim row As DataRow

row = dtTable.NewRow()
row.BeginEdit()
row("AER Number") = "123"
row("Case Version") = "V3"

row.AcceptChanges

The last tow raises an error, "Cannot perform this operation on a row not in
the table."

And if I do not have this line, the row count remains at 0.

What am I doing wrong?

Venki
 
Venki,

You need to add the row to the datatable:

Dim row As DataRow

row = dtTable.NewRow()

row.BeginEdit()
row("AER Number") = "123"
row("Case Version") = "V3"
row.EndEdit()

dtTable.Rows.Add(row)

dtTable.AcceptChanges()

Kerry Moorman
 
Kerry:

Thanks. I realized my mistake.

Kerry Moorman said:
Venki,

You need to add the row to the datatable:

Dim row As DataRow

row = dtTable.NewRow()

row.BeginEdit()
row("AER Number") = "123"
row("Case Version") = "V3"
row.EndEdit()

dtTable.Rows.Add(row)

dtTable.AcceptChanges()

Kerry Moorman
 
Back
Top