4
votes

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.

3

3 Answers

9
votes

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";
1
votes

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).

0
votes

DataRow[] dr = dt.Select("ItemCode ="+ItemCode); {

        dr[0]["ItemName"] = txtItemName.Text;
        dr[0]["ItemName"] = txtItemName.Text;
        dr[0]["GroupCode"] = txtGroup.Text;
        dr[0]["Price"] = txtPrice.Text;
        dr[0]["Descriptions"] = txtDescriptions.Text;
}