DataTable sorting with Datacolumn Name with comma

╄→гoц情女王★ 提交于 2019-12-01 19:39:16

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)

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.

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;
}

So this appears to be a "bug" of sorts in ASP.NET. I have searched several other forums and all have experienced the same problem. FYI, I tried enclosing the column name that contains the comma with various combinations. None of these worked:

[column,name] DESC
['column,name'] DESC
["column,name"] DESC
[column,,name] DESC

So the best method to make this work is this:

table.Columns["column,name"].ColumnName = "Temporary";
table.DefaultView.Sort = "Temporary DESC";
table = table.DefaultView.ToTable();
table.Columns["Temporary"].ColumnName = "column,name";

You could try:

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

Just as a point of interest, this is also how you'd handle it in SQL Server: by putting square brackets around a field that contained reserved characters, such as commas and blank spaces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!