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)
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)
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With