0

したがって、InsertAt() 部分である 1 行を除いて機能するコードのスニペットがここにあります。最後の行をコピーして最初の行として挿入できるかどうかを知りたいです。考え方としてはこう考えてください。おそらくその背後にあるSQLだけで実行できますが、このアプリケーションは非常に古いデータベースとオラクルの両方で機能するように設計されているため、システムが完全に移行されるまで、残念ながらこのように実行する必要があります.

  • 出荷場所 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;

問題: コードは、別のテーブルに属している行を返します。

質問: 最後の行を最初の行にする方法は?

編集の質問: 最後の行を最初の行と最後の行の両方にする方法。(同一行)

4

2 に答える 2

1

追加する行は、すでにそのデータテーブルの一部です。最初にそれを削除する必要があります。簡単なテストで、行を削除するとその行のデータが削除されるように見えるためRemove()InsertAt()機能しないように見えることがわかりました。

ただし、新しい行を作成し、データをその行にコピーして挿入することはできます。その後、古い行を削除できます。たとえば (Linqpad を使用してテスト):

void Main() 
{
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Test", typeof(System.String)));
    var row = dt.NewRow();
    row["Test"] = "1";
    dt.Rows.Add(row);
    row = dt.NewRow();
    row["Test"] = "2";
    dt.Rows.Add(row);
    row = dt.NewRow();
    row["Test"] = "3";
    dt.Rows.Add(row);

    Console.WriteLine("Order before Remove/InsertAt");
    foreach(DataRow rw in dt.Rows)
    {
        Console.WriteLine(rw["Test"]);
    }

    var lastRow = dt.Rows[dt.Rows.Count - 1];
    var newFirstRow = dt.NewRow();
    newFirstRow.ItemArray = lastRow.ItemArray;
    dt.Rows.Remove(lastRow);
    dt.Rows.InsertAt(newFirstRow, 0);

    Console.WriteLine("Order after Remove/InsertAt");
    foreach(DataRow rw in dt.Rows)
    {
        Console.WriteLine(rw["Test"]);
    }
}

予想される出力は次のとおりです。

Order before Remove/InsertAt
1
2
3
Order after Remove/InsertAt
3
1
2
于 2013-01-22T11:37:20.783 に答える
1

新しいものを作成して、必要なDataTable順序で行をインポートできます。

// 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));
}
于 2013-01-22T12:14:43.910 に答える