DataTable.LoadDataRow

  • Thread starter Huang Xin via .NET 247
  • Start date
H

Huang Xin via .NET 247

Below is my code:
'---here create my table
dim Pt_CodeTable as datatable = New DataTable("CodeTable")
With Pt_CodeTable
.Columns.Add("ID", GetType(Int64)) ''?????
.Columns.Add("CODE", GetType(String)) ''???
.Columns.Add("NAME", GetType(String)) ''????
'.Columns.Add("SHORTNAME", GetType(String)) ''???????
.PrimaryKey = New DataColumn() {.Columns("ID"), .Columns("CODE")}

Pi_ID = 37
.Rows.Add(New Object() {Pi_ID, "1", "11"})
.Rows.Add(New Object() {Pi_ID, "2", "10"})
.Rows.Add(New Object() {Pi_ID, "3", "04L"})
.Rows.Add(New Object() {Pi_ID, "4", "04"})
.Rows.Add(New Object() {Pi_ID, "5", "02L"})
.Rows.Add(New Object() {Pi_ID, "6", "05"})
.Rows.Add(New Object() {Pi_ID, "7", "15"})
.Rows.Add(New Object() {Pi_ID, "8", "20"})
End With
Pt_CodeTable.AcceptChanges()
'--------------------------------------------------------------------
Dim DT As DataTable = Pt_CodeTable.Clone
dim LO_ROW as datarow
dim LS_ITEM AS ARRAY
'------------------------Create new table
For Each LO_ROW In Pt_CodeTable.Rows
LS_ITEM = LO_ROW.ItemArray
DT.BeginLoadData()
DT.LoadDataRow(LS_ITEM, False)
DT.EndLoadData()
Next
'-----------------------modify table
Pt_CodeTable.Rows(2)(2) = "AAADDDED"
LS_ITEM = pt_table.Rows(2).ItemArray
DT.BeginLoadData()
DT.LoadDataRow(LS_ITEM, False)
DT.EndLoadData()
'-------------------------
'This time I will get error message, tell me:Adding the row invalidates a constraint
'IN fact, I want to modify column("name") of 3rd row(KEY: ID=37 AND CODE=3) from "04L" to "AAADDDED"
'Tell me how to fix.
 
W

William Ryan eMVP

Huang:

What is the problem I can't tell from your comments?
Huang Xin via .NET 247 said:
Below is my code:
'---here create my table
dim Pt_CodeTable as datatable = New DataTable("CodeTable")
With Pt_CodeTable
.Columns.Add("ID", GetType(Int64)) ''?????
.Columns.Add("CODE", GetType(String)) ''???
.Columns.Add("NAME", GetType(String)) ''????
'.Columns.Add("SHORTNAME", GetType(String)) ''???????
.PrimaryKey = New DataColumn() {.Columns("ID"), ..Columns("CODE")}

Pi_ID = 37
.Rows.Add(New Object() {Pi_ID, "1", "11"})
.Rows.Add(New Object() {Pi_ID, "2", "10"})
.Rows.Add(New Object() {Pi_ID, "3", "04L"})
.Rows.Add(New Object() {Pi_ID, "4", "04"})
.Rows.Add(New Object() {Pi_ID, "5", "02L"})
.Rows.Add(New Object() {Pi_ID, "6", "05"})
.Rows.Add(New Object() {Pi_ID, "7", "15"})
.Rows.Add(New Object() {Pi_ID, "8", "20"})
End With
Pt_CodeTable.AcceptChanges()
'--------------------------------------------------------------------
Dim DT As DataTable = Pt_CodeTable.Clone
dim LO_ROW as datarow
dim LS_ITEM AS ARRAY
'------------------------Create new table
For Each LO_ROW In Pt_CodeTable.Rows
LS_ITEM = LO_ROW.ItemArray
DT.BeginLoadData()
DT.LoadDataRow(LS_ITEM, False)
DT.EndLoadData()
Next
'-----------------------modify table
Pt_CodeTable.Rows(2)(2) = "AAADDDED"
LS_ITEM = pt_table.Rows(2).ItemArray
DT.BeginLoadData()
DT.LoadDataRow(LS_ITEM, False)
DT.EndLoadData()
'-------------------------
'This time I will get error message, tell me:Adding the row invalidates a constraint
'IN fact, I want to modify column("name") of 3rd row(KEY: ID=37 AND
CODE=3) from "04L" to "AAADDDED"
 
Top