Monday, December 5, 2011

add a column at the end of a DataTable and then move it to be the leftmost/first column in the DataTable

public DataTable FixupDataTable(DataTable myDataTable)

{

   myDataTable.Columns.Add("Order Number", typeof(string));

   myDataTable.Rows.ForEach(row =>

   {

      row["Order Number"] = string.Format(

            "<!--{1}--><a href='/Order/Details?id={0}'>{1}</a>", row["Id"], row["OrderNumber"]);

   });

   myDataTable.Columns.Remove("OrderNumber");

   myDataTable.Columns[myDataTable.Columns.Count - 1].SetOrdinal(0);

   return myDataTable;

}

No comments:

Post a Comment