如果我从数据表创建数据行的列表,然后分别对行进行更改.它会反映在数据表中吗? [英] If I create a list of datarow from a datatable and then make changes to the row seaparately. Will it reflect in the datatable?

查看:208
本文介绍了如果我从数据表创建数据行的列表,然后分别对行进行更改.它会反映在数据表中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是点网框架中的新手.所以这是问题所在:

我将用户列表从excel导入到名为"dt"的数据表中.现在,在将dt DataTable传递到的另一个函数中,我正在创建DataRow的列表并按如下所示进行初始化:

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();



它将在LAN ID列中选择所有具有"ABC123"的行.

现在,我将值分配给所选行中的不同列:



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"
}



如果我这样做的话.会使用这些值更新DataTable dt吗?

我尝试过的事情:

由于我正在创建Windows服务,因此调试会很麻烦.因此,在这里问.

另外,如果您可以提供某些原因的背景信息,以便我能理解.我发现有关该主题的大多数文章都是在数据表中添加新行.



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.

推荐答案

是的,但这不是在有限范围内,您必须全局声明您的数据模型

c#-如何在数据表中编辑行-堆栈溢出 [^ ]

如何:编辑数据表中的行 [
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[^]


报价:

如果我这样做的话.会使用这些值更新DataTable dt吗?

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

,只有dt中已过滤的行将更新为新值.

Yes, only the filtered rows in the dt will get updated to new value.

报价:

由于我正在创建Windows服务,因此调试会很麻烦.因此,在这里问.

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";
            }

            
        }
    }
}


这篇关于如果我从数据表创建数据行的列表,然后分别对行进行更改.它会反映在数据表中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆