Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a newbie in dot net framework. So here's the problem:

I imported a list of users from excel to a DataTable named 'dt'. Now in another function that I am passing my dt DataTable to, I am creating a list of DataRow and initializing as below:
List<DataRow> rows = dt.Select([LAN ID]='ABC123').ToList();


It will select all the rows having 'ABC123' in the LAN ID column.

Now, I am assigning values to different columns in the selected rows:
foreach (DataRow row in rows)
{
row[LAN ID] = "DEF456";
row[User ID] = '123456';
row[Last Name] = "Doe";
row[First Name] = "John"
}


If I do as such. Will the DataTable dt be updated with these values?

What I have tried:

Since I am working to create a Windows Service, debugging would be a pain. Hence, asking here.

Also, if you can provide some context as to why so that I can understand. Most of the articles for this topic I found was to add new row in datatable.
Posted
Updated 21-Feb-18 21:01pm

yes it will , but that should not be in limited scope, you have to declare your data model globally

c# - How to Edit a row in the datatable - Stack Overflow[^]

How to: Edit Rows in a DataTable[^]
 
Share this answer
 
Comments
IMFAMOUS 22-Feb-18 3:01am    
As I have mentioned. I am passing Datatable dt to this function. And the function has these codes. So technically dt has a larger scope right? and values will get reflected in dt?
Quote:
If I do as such. Will the DataTable dt be updated with these values?
Yes, only the filtered rows in the dt will get updated to new value.
Quote:
Since I am working to create a Windows Service, debugging would be a pain. Hence, asking here.

You could have created a simple console application and tried.
refer the below
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace CPTemp
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Col1");
            dt.Rows.Add("ABC123");
            dt.Rows.Add("ABC123");
            dt.Rows.Add("test");
            dt.Rows.Add("test123");
            MyMethod(dt);
            string row1 = dt.Rows[0][0].ToString(); //DEF456
            string row2 = dt.Rows[1][0].ToString(); //DEF456
            string row3 = dt.Rows[2][0].ToString(); //test
            string row4 = dt.Rows[3][0].ToString(); //test123

        }

        private static void MyMethod(DataTable dt)
        {
            List<DataRow> rows = dt.Select("Col1='ABC123'").ToList();  // 2 rows

            foreach (DataRow row in rows)
            {
                row["Col1"] = "DEF456";
            }

            
        }
    }
}
 
Share this answer
 
v5
Comments
IMFAMOUS 22-Feb-18 3:07am    
So DataRow only create reference to the DataTable? And yes, i should have done that, was just getting a bit overwhelmed. Thank you for the reply.
Karthik_Mahalingam 22-Feb-18 3:09am    
As I have mentioned. I am passing Datatable dt to this function. And the function has these codes. So technically dt has a larger scope right? and values will get reflected in dt?

updated the solution for this comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900