Set value for all rows in a datatable without for loop
Have you considered removing and re-adding the column with a default value?
dt.Columns.Remove("FilterText")
dt.Columns.Add(New DataColumn() With {.ColumnName = "FilterText",
.DataType = GetType(String),
.DefaultValue = "Jam and chips"})
(assuming your column name is FilterText, and you want to set it to "Jam and chips")
Not unless you count foreach
. One way or another, you'll need to loop.
If you use the DataTable
version, the fastest approach is to use the DataColumn
accessor, i.e.
var col = table.Columns["Foo"];
foreach(var row in table.Rows)
row[col] = value;
As an alternative : since this presumably relates to a database, write the TSQL manually to set all the values appropriately (i.e. with a suitable where
clause in the TSQL).
update [theTable]
set [theColumn] = theValue
where --TODO - something sensible
yes, this is possible, even without loop! This can be done with Expression
property. Keep in mind that string values should be quoted, otherwise it sees this as another columnname.1
This will set the column 'col1' with the string value a
:
preview.Columns["col1"].Expression = "'a'";
This will display in 'col1' the value of the column 'col2':
preview.Columns["col1"].Expression = "col2";