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

C# 如何将新的DataRow添加到DataTable中?,c#,datagridview,datatable,datarow,C#,Datagridview,Datatable,Datarow,我有一个DataGridView绑定到DataTable(DataTable绑定到数据库)。我需要将DataRow添加到DataTable。我正在尝试使用以下代码: dataGridViewPersons.BindingContext[table].EndCurrentEdit(); DataRow row = table.NewRow(); for (int i = 0; i < 28; i++) { row[i] = i.ToString(); } dataGridView

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

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

for (int i = 0; i < 28; i++)
{
    row[i] = i.ToString();
}
dataGridViewPersons.BindingContext[table].EndCurrentEdit();
DataRow行=table.NewRow();
对于(int i=0;i<28;i++)
{
行[i]=i.ToString();
}
但它不起作用,
DataGridView
从未添加新行。请告诉我,我如何修复我的代码


先谢谢你

您可以尝试使用此代码-基于
行。添加方法

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

您必须显式地访问该表

table.Rows.Add(row);
尝试
table.Rows.add(row)在您的
for
语句之后。

这对我很有用:

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

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

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

//َ使用表的结构创建新行:

DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);
//为行中的列提供值(此行应该有28列):

for(int i=0;i<28;i++)
{
行[i]=i.ToString();
}
我觉得很有帮助。新的数据表的代码段从此处开始:

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

请尽量避免将代码作为答案,并解释它的作用和原因。对于没有相关编码经验的人来说,您的代码可能并不明显。请编辑您的答案以包含此代码。此代码不完整,并且与数据行没有明显关系。
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;
}
    GRV.DataSource = Class1.DataTable;
            GRV.DataBind();

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