1

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

现有表

Name        Date         Value        Type

ABC(I)         11/11/2013   12.36      I
DEF(I)         11/11/2013   1          I
GHI(I)          -do-        -do-       I
JKL(P)                                 P
MNO(P)                                 P
PQR(D)                                 D
STU(D)          -d0-        -do-       D

所需表

Name        Date         Value        Type

JKL(P)                                 P
MNO(P)                                 P
PQR(D)                                 D
STU(D)          -d0-        -do-       D
ABC(I)         11/11/2013   12.36      I
DEF(I)         11/11/2013   1          I
GHI(I)          -do-        -do-       I

使用条件

排序应按照列类型。现在我需要对网格视图中显示的行顺序进行一些小的更改。也就是说,付款行将首先出现,然后是所有会费,最后是所有兴趣类型。

我尝试了什么:

  1. 对列进行排序,但这不是我需要的。
  2. 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;
}

我不知道我在哪里以及我缺少/缺少什么。请帮忙。

4

2 回答 2

1

如果我理解正确,您希望先按 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();
    }
于 2013-10-21T08:59:15.730 回答
1

try linq to order your table:

var query = dtGroup.AsEnumerable()
           .OrderBy(c=> c.Field<DateTime?>("Date"))
           .ThenByDescending(c=> c.Field<string>("Name"));
DataView dv2   = query.AsDataView(); 
于 2013-10-21T08:34:11.063 回答