Update DataRow/DataTable in DataSet

  • Thread starter Thread starter SMG
  • Start date Start date
S

SMG

Hi there,
We have a DataSet with one datatable with three columns and multiple
rows. [Each rows has a unique key]

1st Column : Unique Key
2nd Column : Value to Change
3rd Column : Value to Change

Based on some conditions I want to update the values of the 2nd and 3rd
column but want to keep the dataset intact.
It should just change the values.

I tried with first filtering the dataset with select method, but returns an
array of rows which is separate than my dataset.

I want to use the same dataset with the changed values.


Regards,
Shailesh
 
SMG said:
Hi there,
We have a DataSet with one datatable with three columns and multiple
rows. [Each rows has a unique key]

1st Column : Unique Key
2nd Column : Value to Change
3rd Column : Value to Change

Based on some conditions I want to update the values of the 2nd and 3rd
column but want to keep the dataset intact.
It should just change the values.

I tried with first filtering the dataset with select method, but returns
an
array of rows which is separate than my dataset.

Are you sure those rows are separate? Try changing them and see if your
DataSet changes.
I want to use the same dataset with the changed values.

If necessary, you can "manually" loop over the rows:

foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr meets criteria)
{
dr["column1"] = newColumn1;
dr["column2"] = newColumn2;
}
}

John Saunders
 
Hi

Asfar as I remember you can not do that (update specific values in a
dataTable).
you have to first get the dataRow you want to edit, remove it and insert a
new one with the updated values insted.
If mDataTable is the DataTable in your DataSet, then:

get the old row:

DataRow r = mDataTable.Rows[fIndex];

remove it from the dataSet:

mDataTable.Rows.RemoveAt(fIndex);

then, create a new DataRow with the updated values:
DataRow myDataRow = mDataTable.NewRow();
myDataRow["Name"] = newName;
myDataRow["Phone"] = newPhone;

and insert it into the dataSet

this.mDataTable.Rows.InsertAt(myDataRow , fIndex);

and accept the changes if you don't want to keep instance of the old ones.

this.mDataTable.AcceptChanges();

about the Indexs I am not so sure what has to be done, it depends.

hope thats help,
Tal
 
Tal Sharfi said:
Hi

Asfar as I remember you can not do that (update specific values in a
dataTable).
you have to first get the dataRow you want to edit, remove it and insert a
new one with the updated values insted.

This is not the case. You can update the data directly.
If mDataTable is the DataTable in your DataSet, then:

get the old row:

DataRow r = mDataTable.Rows[fIndex];

r["Name"] = newName;
r["Phone"] = newPhone;

r.RowState will be "Modified".

John Saunders
 
Back
Top