[英]How can I update a row in a DataTable in VB.NET?
I have the following code:我有以下代码:
Dim i As Integer = dtResult.Rows.Count
For i = 0 To dtResult.Rows.Count Step 1
strVerse = blHelper.Highlight(dtResult.Rows(i).ToString, s)
' syntax error here
dtResult.Rows(i) = strVerse
Next
I want to add a strVerse
to the current row.我想将
strVerse
添加到当前行。
What am I doing wrong?我究竟做错了什么?
The problem you're running into is that you're trying to replace an entire row object.您遇到的问题是您试图替换整行 object。 That is not allowed by the DataTable API.
DataTable API 不允许这样做。 Instead you have to update the values in the columns of a row object.
相反,您必须更新 object 行的列中的值。 Or add a new row to the collection.
或者向集合中添加新行。
To update the column of a particular row you can access it by name or index.要更新特定行的列,您可以按名称或索引访问它。 For instance you could write the following code to update the column "Foo" to be the value strVerse
例如,您可以编写以下代码将“Foo”列更新为值 strVerse
dtResult.Rows(i)("Foo") = strVerse
You can access columns by index, by name and some other ways :您可以按索引、名称和其他一些方式访问列:
dtResult.Rows(i)("columnName") = strVerse
You should probably make sure your DataTable
has some columns first...您可能应该首先确保您的
DataTable
有一些列......
Dim myRow() As Data.DataRow
myRow = dt.Select("MyColumnName = 'SomeColumnTitle'")
myRow(0)("SomeOtherColumnTitle") = strValue
Code above instantiates a DataRow.上面的代码实例化了一个 DataRow。 Where "dt" is a DataTable, you get a row by selecting any column (I know, sounds backwards).
其中“dt”是一个 DataTable,您可以通过选择任何列来获得一行(我知道,听起来倒退)。 Then you can then set the value of whatever row you want (I chose the first row, or "myRow(0)"), for whatever column you want.
然后你可以为你想要的任何列设置你想要的任何行的值(我选择了第一行或“myRow(0)”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.