C# DataTable最后一行作为第一行?

C# DataTable最后一行作为第一行?,c#,datatable,rows,datarow,C#,Datatable,Rows,Datarow,我这里有一段代码,除了一行是InsertAt()部分外,其他都可以使用。我想知道是否可以复制最后一行并将其作为第一行插入。把这个想法想象成这样。它可能可以在SQL中单独完成,但该应用程序设计用于非常旧的数据库和oracle,因此,不幸的是,在系统完全迁移之前,必须这样做 先前的 装运地点1 装运地点2 装运地点2 之后 起始位置 装运地点1 装运地点2 装运地点3 目的地 代码片段: // Create a DataTable with a list of shipments.

我这里有一段代码,除了一行是InsertAt()部分外,其他都可以使用。我想知道是否可以复制最后一行并将其作为第一行插入。把这个想法想象成这样。它可能可以在SQL中单独完成,但该应用程序设计用于非常旧的数据库和oracle,因此,不幸的是,在系统完全迁移之前,必须这样做

先前的

  • 装运地点1
  • 装运地点2
  • 装运地点2
之后

  • 起始位置
  • 装运地点1
  • 装运地点2
  • 装运地点3
  • 目的地
代码片段:

// Create a DataTable with a list of shipments.    
DataTable dt = c.Query(c.qShipments(Row.Cells[0].Value.ToString()));
// Check if there is at least one shipment
if (dt.Rows.Count >= 1)
{
    // Add the destination of the shipments
    dt.Rows.Add(0, 0, 9999, textBox_CC.Text, textBox_PC.Text, textBox_SL.Text);
    // Add the starting location (which is the same as the destination. It has to be at the top of the DataTable
    dt.Rows.InsertAt(dt.Rows[dt.Rows.Count - 1], 0); // The code

    // Finally calculate and return the object to populate the datagridview with.
    dataGridView_CalculatedRoutes.Rows.Add(x.getRoute(dt));
}
tldr

问题:代码返回属于另一个表的行

问题:如何使最后一行也成为第一行


编辑问题:如何使最后一行同时成为第一行和最后一行。(相同的行)

要添加的行已经是该数据表的一部分。您必须先删除它。在一个简短的测试中,我发现删除一行似乎会删除该行中的数据,因此
Remove()
InsertAt()
似乎不起作用

但是您可以创建一个新行,将数据复制到该行并插入它。之后,您可以删除旧的行。例如(使用Linqpad测试):

预期产出为:

Order before Remove/InsertAt
1
2
3
Order after Remove/InsertAt
3
1
2

您可以创建一个新的
数据表
,并按所需顺序导入行:

// Create a DataTable with a list of shipments.    
DataTable dt = c.Query(c.qShipments(Row.Cells[0].Value.ToString()));
// Check if there is at least one shipment
if (dt.Rows.Count >= 1)
{
    DataTable customDt = new DataTable();

    // Add the starting location (which is the same as the destination. It has to be at 
    customDt.Rows.Add(0, 0, 9999, textBox_CC.Text, textBox_PC.Text, textBox_SL.Text);

    foreach(DataRow row in dt.Rows)
    {
        customDt.ImportRow(row);
    }
    // Add the destination of the shipments
    customDt.ImportRow(customDt.Rows[0]);

    // Finally calculate and return the object to populate the datagridview with.
    dataGridView_CalculatedRoutes.Rows.Add(x.getRoute(dt));
}

这将给出最后一行
DataRow last=table.Rows[table.Rows.Count-1]我已经尝试过了,甚至尝试过更改“last”的一个值。但是它一直在返回已经属于这个DataTable的数据。希望这有助于预期的输出应该是123x123x。它们是相同的行。@DavideNguyen:好的,那么您想输入两条记录,第一条记录,第二条记录,最后一条记录?@DavideNguyen这只是一个例子。如果您只是通过删除Remove()-命令来修改我的示例,您将得到3123作为输出,这基本上就是您想要的。你只需要将它应用到你自己的代码中……啊,我想我现在看到了区别!谢谢,我会继续的!这可能是一个更好的解决方案。我不知道这是一个重要的功能。我会对解决方案进行加权,然后选择其中一个。这个方法非常有效,而且感觉很干净。谢谢
// Create a DataTable with a list of shipments.    
DataTable dt = c.Query(c.qShipments(Row.Cells[0].Value.ToString()));
// Check if there is at least one shipment
if (dt.Rows.Count >= 1)
{
    DataTable customDt = new DataTable();

    // Add the starting location (which is the same as the destination. It has to be at 
    customDt.Rows.Add(0, 0, 9999, textBox_CC.Text, textBox_PC.Text, textBox_SL.Text);

    foreach(DataRow row in dt.Rows)
    {
        customDt.ImportRow(row);
    }
    // Add the destination of the shipments
    customDt.ImportRow(customDt.Rows[0]);

    // Finally calculate and return the object to populate the datagridview with.
    dataGridView_CalculatedRoutes.Rows.Add(x.getRoute(dt));
}