DataTable排序,Datacolumn名称带逗号 [英] DataTable sorting with Datacolumn Name with comma

查看:131
本文介绍了DataTable排序,Datacolumn名称带逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,显示了不同时期不同城市的统计数据.它具有以下列名称

I have data table showing statistics of different cities for different periods. It has following columns names

Period | city1,state | city2,state | city3,state
Jan        15              25          20 
Feb        5              26          29 
Mar        35              27          21 

我已经应用了一些逻辑,它为我提供了列名,以对各个列名进行排序,并再次将数据与前端的网格绑定.

I have applied some logic and it gives me column name to sort the respective column name and bind the data again with grid on front End.

现在,当我尝试以下代码进行排序时出现问题

Now problems comes when I tried the following code for sorting

griData.DefaultView.Sort = string.Format("{0} {1}", orderByField, sortDirection)

由于列名中的逗号,将其视为两列并给出异常. 示例:如果我按city1,state列进行升序排序,则排序表达式将为 city1,state asc ,并且在执行该语句时会抛出异常,即city1列不会不存在而不是排序.谢谢

Because of the comma in the column name it treats it as two columns and give exception. Example: If I am sorting by column city1,state in ascending order then sort expression will be city1,state asc and on executing the statement it throws an exception that column city1 doesn't exist instead of sorting. Thanks

推荐答案

尝试以这种方式更改格式

Try to change your format in this way

griData.DefaultView.Sort = string.Format("[{0}] {1}", orderByField, sortDirection)

对待包含保留字符的列名称的通常方法是将名称括在方括号中.

The usual way to treat column names that contains reserved characters is to enclose the name in square brackets.

编辑,我找不到这种情况的解决方法. (拥有这样的名称当然是一个错误的决定,但是老实说,我坚信方括号可以解决这个问题)

EDIT I am unable to find a workaround for this case. (Of course it is a bad decision to have this kind of names, but honestly, I was convinced that the square brackets could handle that)

到目前为止,找到的唯一可能的解决方法是以某种方式更改查询,为列名创建别名,然后可以对这些别名进行排序.像这样

The only possibile workaround, found so far, is to change, in some way, your query, creating an alias for your column names and then you could sort for these alias. Something like this

SELECT Period, 
      [city1,state] AS City1State, 
      [city2,state] AS City2State, 
      [city3,state] AS City3State
FROM yourTable

....

orderByField = "City1State"
sortDirection = "DESC"
griData.DefaultView.Sort = string.Format("{0} {1}", orderByField, sortDirection)

再次编辑:您的问题确实引起了我的好奇,所以我搜索了DataView的Sort属性所使用的代码,并找到了一种内部方法,该方法似乎是有罪的.它在逗号处分割排序字符串,并且完全忽略该字符串周围的任何方括号.因此,似乎没有办法使用该名称.

EDIT AGAIN Your question has really hit my curiosity so I have searched the code used for the Sort property of a DataView and reached an internal method that seems to be the guilty one. It splits the sort string at the comma and it ignores altogether any square brackets put around the string. So, it seems that there is no way to use that name.

DataView的Sort属性的代码.....

Code for the Sort property of the DataView.....

internal unsafe IndexField[] ParseSortString(string sortString)
{
    string str;
    int num;
    int num2;
    string[] strArray;
    IndexField[] fieldArray;
    DataColumn column;
    bool flag;
    char[] chArray;
    fieldArray = zeroIndexField;
    if (sortString == null)
    {
        goto Label_011A;
    }
    if (0 >= sortString.Length)
    {
        goto Label_011A;
    }

    // Here the split on the comma char (0x2C) ignoring the fact that 
    // the whole sort expression is inside square brackets????

    strArray = sortString.Split(new char[] { 0x2c });
    fieldArray = new IndexField[(int) strArray.Length];
    num2 = 0;
    goto Label_0111;
Label_0041:
    str = strArray[num2].Trim();
    num = str.Length;
    flag = 0;
    if (num < 5)
    {
        goto Label_007D;
    }
    if (string.Compare(str, num - 4, " ASC", 0, 4, 5) != null)
    {
        goto Label_007D;
    }
    str = str.Substring(0, num - 4).Trim();
    goto Label_00A7;
Label_007D:
    if (num < 6)
    {
        goto Label_00A7;
    }
    if (string.Compare(str, num - 5, " DESC", 0, 5, 5) != null)
    {
        goto Label_00A7;
    }
    flag = 1;
    str = str.Substring(0, num - 5).Trim();
Label_00A7:
    if (str.StartsWith("[", 4) == null)
    {
        goto Label_00DE;
    }
    if (str.EndsWith("]", 4) == null)
    {
        goto Label_00D5;
    }
    str = str.Substring(1, str.Length - 2);
    goto Label_00DE;
Label_00D5:
    throw ExceptionBuilder.InvalidSortString(strArray[num2]);
Label_00DE:
    column = this.Columns[str];
    if (column != null)
    {
        goto Label_00F7;
    }
    throw ExceptionBuilder.ColumnOutOfRange(str);
Label_00F7:
    *(&(fieldArray[num2])) = new IndexField(column, flag);
    num2 += 1;
Label_0111:
    if (num2 < ((int) strArray.Length))
    {
        goto Label_0041;
    }
Label_011A:
    return fieldArray;
}

这篇关于DataTable排序,Datacolumn名称带逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆