c#如何更改 DataTable 列的顺序
dataTable.Columns["Qty"].SetOrdinal(0); dataTable.Columns["Unit"].SetOrdinal(1);
扩展方法:
public static class DataTableExtensions { public static void SetColumnsOrder(this DataTable table, params String[] columnNames) { int columnIndex = 0; foreach(var columnName in columnNames) { table.Columns[columnName].SetOrdinal(columnIndex); columnIndex++; } } }
用法:
table.SetColumnsOrder("Qty", "Unit", "Id");
或
table.SetColumnsOrder(new string[]{"Qty", "Unit", "Id"});
微信号:jamesworkshop 学习QQ群:364976091