Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding values to specific DataTable cells

I'm wondering if it's possible to add values to specific DataTable cells?

Suppose I have an existing dataTable and I add a new column, how would I go about adding to the new column's rows without overwriting the existing columns' rows?

As far as I'm aware, there isn't a method for adding to specific cells (unless I'm wrong).

  dt.Rows.Add(a, b, c, d) 

where a, b, c and d are string values. So what if I just want to add to the d column?

Any help would be appreciated.

like image 480
Winz Avatar asked Sep 06 '12 02:09

Winz


People also ask

How do you add data to existing rows in DataTable in C#?

After you create a DataTable and define its structure using columns and constraints, you can add new rows of data to the table. To add a new row, declare a new variable as type DataRow. A new DataRow object is returned when you call the NewRow method.

How do you append to a DataTable?

On the Home tab, in the View group, click View, and then click Design View. On the Design tab, in the Query Type group, click Append. The Append dialog box appears. Next, you specify whether to append records to a table in the current database, or to a table in a different database.


1 Answers

If it were a completely new row that you wanted to only set one value, you would need to add the whole row and then set the individual value:

DataRow dr = dt.NewRow(); dr[3].Value = "Some Value"; dt.Rows.Add(dr); 

Otherwise, you can find the existing row and set the cell value

DataRow dr = dt.Rows[theRowNumber]; dr[3] = "New Value"; 
like image 82
pinkfloydx33 Avatar answered Sep 18 '22 13:09

pinkfloydx33