C# 在运行时重新排列Datatable的行

C# 在运行时重新排列Datatable的行,c#,asp.net,gridview,datatable,C#,Asp.net,Gridview,Datatable,我在一个数据表中有多行,请参见下面的示例: 现有表格 必选表格 使用条件 应根据列类型进行排序。现在,我需要对gridview中显示的行的顺序进行一个小的更改。也就是说,付款行将首先出现,然后是所有应付款项,最后是所有类型的利息 我尝试的是: 列的排序,但这不是我需要的 Tim Schmelter建议的自定义分组 代码是: public DataTable GroupBy(string i_sGroupByColumn, string i_sAggregateColumn, DataTable

我在一个数据表中有多行,请参见下面的示例:

现有表格 必选表格 使用条件 应根据列类型进行排序。现在,我需要对gridview中显示的行的顺序进行一个小的更改。也就是说,付款行将首先出现,然后是所有应付款项,最后是所有类型的利息

我尝试的是:
  • 列的排序,但这不是我需要的
  • Tim Schmelter建议的自定义分组
  • 代码是:

    public DataTable GroupBy(string i_sGroupByColumn, string i_sAggregateColumn, DataTable i_dSourceTable)
    {
    
        DataView dv = new DataView(i_dSourceTable);
    
        //getting distinct values for group column
        DataTable dtGroup = dv.ToTable(true, new string[] { i_sGroupByColumn });
    
        //adding column for the row count
        dtGroup.Columns.Add("Count", typeof(int));
    
        //looping thru distinct values for the group, counting
        foreach (DataRow dr in dtGroup.Rows) {
            dr["Count"] = i_dSourceTable.Compute("Count(" + i_sAggregateColumn + ")", i_sGroupByColumn + " = '" + dr[i_sGroupByColumn] + "'");
        }
    
        //returning grouped/counted result
        return dtGroup;
    }
    

    我不知道我在哪里,缺少什么。请帮忙。

    请尝试linq订购您的餐桌:

    var query = dtGroup.AsEnumerable()
               .OrderBy(c=> c.Field<DateTime?>("Date"))
               .ThenByDescending(c=> c.Field<string>("Name"));
    DataView dv2   = query.AsDataView(); 
    
    var query=dtGroup.AsEnumerable()
    .OrderBy(c=>c.Field(“日期”))
    .然后按降序排列(c=>c.字段(“名称”));
    DataView dv2=query.AsDataView();
    
    如果我理解正确,您希望先按p、D、I排序,然后按日期排序

        Dictionary<string, int> sortDictionary = new Dictionary<string, int>();
        sortDictionary.Add("P", 1);
        sortDictionary.Add("D", 2);
        sortDictionary.Add("I", 3);
    
    
        var q = from row in dtGroup.AsEnumerable()
                let type = sortDictionary[row.Field<string>("Name").Substring(4, 1)]
                orderby type, row.Field<string>("Name")
                select row;
    
        foreach (var r in q)
        {
            string x = r["Name"].ToString() + r["Date"].ToString();
        }
    
    Dictionary sortDictionary=newdictionary();
    添加(“P”,1);
    sortDictionary.添加(“D”,2);
    sortDictionary.添加(“I”,3);
    var q=来自dtGroup.AsEnumerable()中的行
    let type=sortDictionary[row.Field(“Name”).Substring(4,1)]
    订单类型,行字段(“名称”)
    选择行;
    foreach(q中的var r)
    {
    字符串x=r[“名称”].ToString()+r[“日期”].ToString();
    }
    
    OK,您可以通过将row.Field(“日期”)更改为row.Field(“名称”)来更改orderby。var q是您要查找的数据表
    var query = dtGroup.AsEnumerable()
               .OrderBy(c=> c.Field<DateTime?>("Date"))
               .ThenByDescending(c=> c.Field<string>("Name"));
    DataView dv2   = query.AsDataView(); 
    
        Dictionary<string, int> sortDictionary = new Dictionary<string, int>();
        sortDictionary.Add("P", 1);
        sortDictionary.Add("D", 2);
        sortDictionary.Add("I", 3);
    
    
        var q = from row in dtGroup.AsEnumerable()
                let type = sortDictionary[row.Field<string>("Name").Substring(4, 1)]
                orderby type, row.Field<string>("Name")
                select row;
    
        foreach (var r in q)
        {
            string x = r["Name"].ToString() + r["Date"].ToString();
        }