Select and updating data from datatable

user2871190 picture user2871190 · Jan 7, 2014 · Viewed 23.1k times · Source

I select data from my datatable, by using following code:

DataRow[] result = table.Select("Size >= 230 AND Sex = 'm'");

Now I change the data in the datarow-array result and I want to update my datatable (datatable should get the changes). Which is the easiest way to do that?

In VB6 I could simply set a filter, on the recordset, edit my rows and simply save my changes. Is there a similar way using DataTables?

EDIT:

I have an additional question. What, if I wanna add a new row and I want to reuse the same code?

For example like that:

filteredRows = myDataset.Tables[0].Select("select where id = 1");
if (filteredRow.Lenght == 0) {
filteredRows = myDataset.Tables[0].NewRow();
}
// I wanna use this code, no matter if I edit a row, or if it is a new row.
filteredRows[index]["Name"] = "Max";
filteredRows[index]["Address"] = "Random Address";
filteredRows[index]["WhatEver"] = "...";
//...

I tried this way, but it doesn't affect the original dataset.

Answer

Vishal Patel picture Vishal Patel · Jan 7, 2014

This is one way to update datatable data....

DataRow[] HRow = dataSet1.Tables["Human"].Select("Size >= 230 AND Sex = 'm'");

HRow[0]["Size"] = 230;

HRow[0]["Sex"] = "m";