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.
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.
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.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With