【问题标题】:How to add new DataRow into DataTable?如何将新的 DataRow 添加到 DataTable 中?
【发布时间】:2012-09-28 14:32:17
【问题描述】:

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

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

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

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

提前谢谢你。

【问题讨论】:

标签: c# datagridview datatable datarow


【解决方案1】:

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

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

参考:https://docs.microsoft.com/dotnet/api/system.data.datarowcollection.add?view=net-5.0#System_Data_DataRowCollection_Add_System_Data_DataRow_

【讨论】:

    【解决方案2】:

    我发现dotnetperls examples on DataRow 很有帮助。从那里为新的DataTable 编码 sn-p:

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

    【讨论】:

      【解决方案3】:

      //َ用表的结构新建一行:

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

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

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

      【讨论】:

        【解决方案4】:

        你必须明确add the row到表

        table.Rows.Add(row);
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

            这对我有用:

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

            【讨论】:

              【解决方案7】:

              for 声明之后尝试table.Rows.add(row);

              【讨论】:

                【解决方案8】:
                    GRV.DataSource = Class1.DataTable;
                            GRV.DataBind();
                
                Class1.GRV.Rows[e.RowIndex].Delete();
                        GRV.DataSource = Class1.DataTable;
                        GRV.DataBind();
                

                【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-01-30
                • 2012-05-03
                • 2011-06-15
                • 2012-11-25
                • 1970-01-01
                • 1970-01-01
                • 2012-02-06
                • 2010-11-22
                相关资源
                最近更新 更多