Mati
Mati

Reputation: 511

Set value of each DataTable row in specific column

I'm having some problem while trying to set column value.

I'v had a dataTable which get some values from SQL and then im adding two new columns by :

        dataTable.Columns.Add("dest", typeof(int));

        dataTable.Columns.Add("amount", typeof(int));

Which works great but now i want to put 0 in every row in column name dest - and later user will edit this, and then i want to set amount value as

       amount = all(this column is in dataTable before I add these 2 columns) + dest;

Upvotes: 3

Views: 17387

Answers (2)

AmirHossein Manian
AmirHossein Manian

Reputation: 617

You can use foreach too.

 foreach (DataRow row in myDataTable.Rows)
     //if (row["X"] has condition) // or if any condition
         row["colName"] = row[colIndex] = "abc";

Upvotes: 1

Nick Bull
Nick Bull

Reputation: 9876

int columnNumber = 5; //Put your column X number here

for (int i = 0; i < yourDataTable.Rows.Count; i++)
{
    yourDataTable.Rows[i][columnNumber] = "0"; 
}

Upvotes: 3

Related Questions