一般情况下DataTable里排序都是通过DataView.Sort对列进行排序,DataTable如何对行进行排序,比如:3 (列名)   1(列名)    2(列名)
c1           a1           b1
c2           a2           b2如何变成
1 (列名)   2(列名)    3(列名)
a1           b1           c1
a2           b2           c2
给点代码或者思路

解决方案 »

  1.   


            public static DataTable SelectDistinct(this DataTable SourceTable, params string[] FieldNames)
            {
                object[] lastValues;
                DataTable newTable;
                DataRow[] orderedRows;            if (FieldNames == null || FieldNames.Length == 0)
                    throw new ArgumentNullException("FieldNames");            lastValues = new object[FieldNames.Length];
                newTable = new DataTable(SourceTable.TableName);
                //下面的foreach方法修改过
                foreach (DataColumn column in SourceTable.Columns)
                    newTable.Columns.Add(column.ColumnName, column.DataType);            orderedRows = SourceTable.Select("", string.Join(",", FieldNames));            foreach (DataRow row in orderedRows)
                {
                    if (!fieldValuesAreEqual(lastValues, row, FieldNames))
                    {
                        newTable.Rows.Add(createRowClone(row, newTable.NewRow(), SourceTable.Columns));//此处调用修改过                    setLastValues(lastValues, row, FieldNames);
                    }
                }            return newTable;
            }        private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
            {
                bool areEqual = true;            for (int i = 0; i < fieldNames.Length; i++)
                {
                    if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
                    {
                        areEqual = false;
                        break;
                    }
                }            return areEqual;
            }        private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, DataColumnCollection dcc)
            {
                foreach (DataColumn column in dcc)
                    newRow[column.ColumnName] = sourceRow[column.ColumnName];            return newRow;
            }        private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
            {
                for (int i = 0; i < fieldNames.Length; i++)
                    lastValues[i] = sourceRow[fieldNames[i]];
            } 
      

  2.   

    如果要修改列的位置,你需要创建一个新的DataTable,重新按照固定顺序添加列后,再从现有的DataTable将内容Merge过去即可。
      

  3.   


    有个操作是必须和列顺序有关的,当使用SQLSERVER2008中的用户自定义表类型传递DataTable给SP更新数据库时,就必须处理好列顺序。估计是微软的BUG,那个传递是不看列名,只看列在DataTable里面出现的先后顺序。
      

  4.   

    select  列名=name  from   syscolumns   where   id=object_id(N'表名') order by name
      

  5.   

    默认取得 table 的 Columns 中字段的顺序DataView 中通过 PropertyDescriptorCollection ITypedList.GetItemProperties (
    PropertyDescriptor[] listAccessors
    )检索 table 的列, 返回属性描述符集合,看来你要定制这个过程.感觉没有必要这么做, 你的顺序只是 UI 上的顺序.可以在外面绑定 PropertyDescriptor 的时候进行顺序选择.
      

  6.   

    DataView view=dataTable.DefaultView
    view.Filter="列名 asc";
    DataTable tab=view.ToTable();