Share via


Find and Update cell in DataTable

First statement use of LINQ query

-

  • // Get all DataRows where the value is the name you want.
  • // dt is your datatable object,which cell you want update

IEnumerable<DataRow> rows = dt.Rows.Cast<DataRow>().Where(r => r["Name"].ToString() == "SomeName");

dt.Rows.Cast<DataRow>().Where(r => r["ZipCode"].ToString() this part related to your datatable

  • "SomeName" this one is your compare value

  • // Loop through the rows and change the name.

  • rows.ToList().ForEach(r => r.SetField("Name", "your value"));

  •  r.SetField("Here your datatable column name","Here your value whatever you want update"

**// Alternative approach.

// Simply loop through the rows, check the value of the Name field and change its value accordingly.
**

foreach (DataRow row in dt.Rows)

{

if (row["ZipCode"].ToString() == "Your Value")

row.SetField("ZipCode", "your value");