如何在1行代码中为c#DataTable添加新行? [英] How to add a new row to c# DataTable in 1 line of code?

查看:432
本文介绍了如何在1行代码中为c#DataTable添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用一行代码在c#中的datatable中添加新行?我只是把一些数据放在一个测试中,看起来很慢就要写这样的东西:

Is it possible to add a new row to a datatable in c# with just 1 line of code? I'm just dummying up some data for a test and it seems pretty slow to have to write something like this:

DataTable dt= new DataTable("results");
DataRow dr1 = dt.NewRow();
dr1[0] = "Sydney";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2[0] = "Perth";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3[0] = "Darwin";
dt.Rows.Add(dr3);

我假设你可以像下面的代码那样做,但是找不到正确的语法。

I was assuming you could do something like the code below, but I can't find the correct syntax.

dt.Rows.Add(dt.NewRow()[0]{"Sydney"});
dt.Rows.Add(dt.NewRow()[0]{"Perth"});
dt.Rows.Add(dt.NewRow()[0]{"Darwin"});

是的,我知道在写这个问题的时候我可以完成编码很长的路,而不是拖延:)

And yes I know in the time I've taken to write this question I could have finished coding it the long way instead of procrastinating about it :)

谢谢!

推荐答案

p>是的,您可以执行以下操作:

Yes, you can do the following:

dt.Rows.Add("Sydney");

这篇关于如何在1行代码中为c#DataTable添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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