Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select and updating data from datatable

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.

like image 720
user2871190 Avatar asked Dec 26 '22 15:12

user2871190


2 Answers

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";
like image 177
Vishal Patel Avatar answered Dec 28 '22 08:12

Vishal Patel


I have the idea that when you update a row from a DataTable.Select, datatable knows the changes but I'm not sure since I never faced something like that. I think Datarow.AcceptChanges will do what you want. But check for sure that you really have problem like that before using (AcceptChanges).

like image 31
Emmanouil Chountasis Avatar answered Dec 28 '22 08:12

Emmanouil Chountasis