如何将新的DataRow添加到DataTable? [英] How to add new DataRow into DataTable?

查看:462
本文介绍了如何将新的DataRow添加到DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView 绑定到 DataTable DataTable 绑定到数据库)。我需要在 DataTable 中添加 DataRow 。我正在尝试使用以下代码:

I have a DataGridView binded to a DataTable (DataTable binded to database). I need to add a DataRow to the DataTable. I'm trying to use the following code:

dataGridViewPersons.BindingContext[table].EndCurrentEdit();
DataRow row = table.NewRow();

for (int i = 0; i < 28; i++)
{
    row[i] = i.ToString();
}

但是它不起作用, DataGridView 从未添加过新行。请告诉我,如何解决我的代码?

But it doesn't work, DataGridView has never been added a new row. Please, tell me, how can I fix my code?

谢谢。

推荐答案

您可以尝试使用此代码-基于 Rows.Add方法

You can try with this code - based on Rows.Add method

DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);

链接: https://msdn.microsoft.com/zh-CN/library/9yfsd47w.aspx

这篇关于如何将新的DataRow添加到DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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