Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

What am I doing wrong?

like image 227
Tola Avatar asked Mar 05 '09 04:03

Tola


People also ask

How to update DataTable in vb net?

We can update the data in a DataView . The following source code shows how to update data in a DataView . Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Code on button click event.

How add data row to DataTable?

To add a new row, declare a new variable as type DataRow. A new DataRow object is returned when you call the NewRow method. The DataTable then creates the DataRow object based on the structure of the table, as defined by the DataColumnCollection.

Can we update DataTable in C#?

use SetModified() method to update data table value.

What is row in DataTable?

Description. Working with rows is a fundamental part of DataTables, and you want to be able to easily select the rows that you want from the table. This method is the row counterpart to the columns() and cells() methods for working with columns and cells in the table, respectively.


1 Answers

The problem you're running into is that you're trying to replace an entire row object. That is not allowed by the DataTable API. Instead you have to update the values in the columns of a row 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

dtResult.Rows(i)("Foo") = strVerse
like image 99
JaredPar Avatar answered Sep 20 '22 15:09

JaredPar