9

Let suppose there are three columns in my DataTable

  1. code

  2. name

  3. color

If I know the code and name, how can I update the color of that specific row whose code and name match my criteria? I want to do this without using Loops!

3
  • 3
    Why in the world do you want to do this without using loops? Somewhere, a loop will be required. Even if you use LINQ, a loop is still performed internally.
    – Cody Gray
    Commented May 21, 2011 at 14:44
  • Cody, the OP wants to update 1 row. A Dictionary wouldn't require a loop. Commented May 21, 2011 at 20:20
  • A table is not a dictionary Commented Dec 19, 2012 at 22:49

5 Answers 5

17

You can use LINQ:

DataRow dr = datatable.AsEnumerable().Where(r => ((string)r["code"]).Equals(someCode) && ((string)r["name"]).Equals(someName)).First();
dr["color"] = someColor;

Of course I'm assuming all those criteria are strings. You should change the casts to the correct types.

3
  • Now i am getting error at Data.EnumerableRowCollection does not contain .First() Also we are just updating a row how will reflect these changes in DataTable.
    – Zain Ali
    Commented May 21, 2011 at 11:09
  • Make sure you're using System.Linq, but Where() shouldn't be working either if that wasn't being referenced. By "reflect the changes" do you mean seeing them on the screen? You'll need to re-bind the DataTable to whatever you're using to display it.
    – Farinha
    Commented May 21, 2011 at 11:33
  • This is fine, as far as it goes...however, if the user wants to update multiple rows...this won't work.
    – JasonG
    Commented Dec 1, 2023 at 23:29
13
// Use the Select method to find all rows matching the name and code.
DataRow[] rows = myDataTable.Select("name 'nameValue' AND code = 'codeValue');

for(int i = 0; i < rows.Length; i ++)
{
      rows[i]["color"] = colorValue;
}
3
  • How this updated row will be reflected back(Commited) to myDataTable
    – Zain Ali
    Commented Jun 7, 2011 at 11:56
  • @Zain:This already does the update. Use the Select method of the DataTable to locate a specific row and assign new values to the desired columns. That's it.
    – CharithJ
    Commented Jun 7, 2011 at 23:04
  • should be DataRow[] rows = myDataTable.Select("name = 'nameValue' AND code = 'codeValue')
    – lstanczyk
    Commented Jul 5, 2012 at 17:25
5
DataTable recTable = new DataTable();

// do stuff to populate table

recTable.Select(string.Format("[code] = '{0}' and [name] = '{1}'", someCode, someName)).ToList<DataRow>().ForEach(r => r["Color"] = colorValue);
1
  • This is still a loop
    – Neil Busse
    Commented Feb 1, 2017 at 15:58
0

With LINQ:

var dataRows = dt.AsEnumerable().Select(c => { c["color"] = c["Code"].ToString() == "1" ? "Red" : "White"; return c; });
dt = dataRows.CopyToDataTable();
-3

You could do:

 foreach (DataRow row in datatable.Rows)
        {
            if(row["code"].ToString() == someCode && row["name"].ToString() == someName)

              {
                  row["color"] = someColor;
              }


        }
2
  • 3
    -1 -> The poster stated in his question to not use loops. I understand this works but it already breaks the criteria of the question from the get go.
    – IyaTaisho
    Commented Aug 12, 2014 at 14:27
  • If the request criteria make no sense, a 'textbook' solution like this one by @Darren is much appreciated by other possible users. Any LINQ routine uses loops inside, and they are far less efficient than a simple for-loop. +1 upvote . Commented Dec 13, 2021 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.