C# 如何将新的 DataRow 添加到 DataTable 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12642049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:59:32  来源:igfitidea点击:

How to add new DataRow into DataTable?

c#datagridviewdatatabledatarow

提问by malcoauri

I have a DataGridViewbinded to a DataTable(DataTablebinded to database). I need to add a DataRowto the DataTable. I'm trying to use the following code:

我有一个DataGridView绑定到DataTableDataTable绑定到数据库)。我需要DataRowDataTable. 我正在尝试使用以下代码:

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

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

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

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

Thank you in advance.

先感谢您。

采纳答案by Aghilas Yakoub

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

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

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

Link : https://msdn.microsoft.com/en-us/library/9yfsd47w.aspx

链接:https: //msdn.microsoft.com/en-us/library/9yfsd47w.aspx

回答by rene

You have to add the rowexplicitly to the table

您必须将行显式添加到表中

table.Rows.Add(row);

回答by Riccardo

try table.Rows.add(row);after your forstatement.

table.Rows.add(row);在你的for陈述之后尝试。

回答by x-silencer

This works for me:

这对我有用:

var table = new DataTable();
table.Rows.Add();

回答by Aleksey Kontsevich

If need to copy from another table then need to copy structure first:

如果需要从另一个表复制,则需要先复制结构:

DataTable copyDt = existentDt.Clone();
copyDt.ImportRow(existentDt.Rows[0]);

回答by Mehran Sarrafi

//?Creating a new row with the structure of the table:

//?用表的结构创建一个新行:

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

//Giving values to the columns of the row(this row is supposed to have 28 columns):

//给行的列赋值(该行应该有 28 列):

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

回答by mimo

I found dotnetperls examples on DataRowvery helpful. Code snippet for new DataTablefrom there:

我发现dotnetperls 示例DataRow非常有帮助。DataTable来自那里的新代码片段:

static DataTable GetTable()
{
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("Weight", typeof(int));
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Breed", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now);
    table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now);
    table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now);
    table.Rows.Add(25, "Charles", "Cavalier King Charles Spaniel", DateTime.Now);
    table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now);

    return table;
}

回答by LollyPop

    GRV.DataSource = Class1.DataTable;
            GRV.DataBind();

Class1.GRV.Rows[e.RowIndex].Delete();
        GRV.DataSource = Class1.DataTable;
        GRV.DataBind();