Sorting a DataTable named Datacolumn with a comma

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

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

I applied some logic and this gives me a column name to sort the corresponding column name and bind the data again to the front grid.

Now problems arise when I tried the following code to sort

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

Because of the comma in the column name, it treats it as two columns and gives an exception. Example . If I sort by city1 column, specify in ascending order, then the sort expression will be city1, state asc and when executing the instruction it will throw an exception, which city1 column doesn’t instead of sorting. Thanks

+4
source share
3 answers

Try changing the format this way.

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

A common way to handle reserved column names is to enclose the name in square brackets.

EDIT I cannot find a workaround for this case. (Of course, this is a bad decision to have such names, but to be honest, I was convinced that the square brackets could handle this)

The only possible solution found so far is to somehow modify your query by creating an alias for your column names, and then you can sort for those aliases. Something like that

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)

, , Sort DataView, , . . , , .

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

, "" ASP.NET. , . FYI, , . :

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

, :

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

:

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

, , SQL Server: , , .

+1

Source: https://habr.com/ru/post/1529418/


All Articles