DataTable.ImportRow 不添加行 [英] DataTable.ImportRow is not adding rows

查看:22
本文介绍了DataTable.ImportRow 不添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 DataTable,然后向其中添加几行.这是我的代码:

I'm trying to make a DataTable and then add a couple rows to it. Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;


namespace thisNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt=new DataTable();
            dt.Columns.Add("XYZID");
            DataRow dr=dt.NewRow();
            dr["XYZID"]=123;
            dt.ImportRow(dr);
            dr["XYZID"] = 604303;
            dt.ImportRow(dr);

        }
    }
}

当我逐步执行程序时,dr 成功初始化并填充了值,但是在 ImportRow(dr) 之后,dt 中的行数 仍然是 0.我觉得我一定遗漏了一些明显的东西.这里出了什么问题?

When I step through the program, dr is successfully initialized and populated with values, but then after ImportRow(dr), the count of rows in dt is still 0. I feel like I must be missing something obvious. What's going wrong here?

推荐答案

试试这个代码:

dt.Rows.Add(dr)

这篇关于DataTable.ImportRow 不添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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