Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Datarow field value

Tags:

c#

excel

datarow

First I have last update file from DB

DataTable excelData = ReadSCOOmega(lastUploadFile);

, after this iterate over this data

foreach (DataRow currentRow in rows)
{
     currentRow.
}

Is that possible to change da data in the foreach loop.I can access only the value of this data

currentRow.Field<object>("Some column name")

but not to change it.My idea is selected.I have a multiple deals in excel file and when is upload to DB, I need to make changes in this file.Is that possible or I need to store the data in other collection?

like image 412
D Todorov Avatar asked May 10 '16 17:05

D Todorov


People also ask

How do you DataRow in C#?

To create a new DataRow, use the NewRow method of the DataTable object. After creating a new DataRow, use the Add method to add the new DataRow to the DataRowCollection. Finally, call the AcceptChanges method of the DataTable object to confirm the addition.

How do I add a row to DataRow?

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.

How is DataRow defined?

A DataRow is created by calling the NewRow( ) method of a DataTable , a method that takes no arguments. The DataTable supplies the schema, and the new DataRow is created with default or empty values for the fields: // create a table with one column DataTable dt = new DataTable(); dt. Columns.


2 Answers

You can do that like :

foreach (DataRow currentRow in excelData.Rows)
{
    currentRow.BeginEdit();
    currentRow["ColumnName"] = value;
    //.....
    currentRow.EndEdit();
}
like image 145
Abdellah OUMGHAR Avatar answered Sep 22 '22 13:09

Abdellah OUMGHAR


You can use indexer to set the data stored to the field: currentRow["columnName"] = value.

See MSDN DataRow.Item Property

like image 20
V.Leon Avatar answered Sep 22 '22 13:09

V.Leon