I have 2 DataTables that I'm combining using the .Merge
method.
The column type for the 2 DataTables are System.Decimal
.
But it does not seem to sort properly. The data for the 1st DataTable will be sorted first and then subsequently the data from the 2nd DataTable.
So if you see the picture above, it will display the 1st DataTable data, and then 2nd DataTable, sorted by the start time.
If I reverse the sorting, it will sort the 2nd DataTable data in descending order then the 1st DataTable will appear.
This is the code for my sorting
DataView dv = new DataView(dataTableResult);
dv.Sort = string.Format("{0} {1}", ViewState["sortexp"].ToString(),
GetSortDirection());
return dv;
ViewState["sortexp"]
contains Start Time in this case, and GetSortDirection
is either "ASC" or "DESC".
My query:
select ....
to_number(to_char([start_field],'hh24mi')) as start_time,
to_number(to_char([end_field], 'hh24mi')) as end_time,
...
from [table]
where [condition]
and another one I am getting the result from webservice and populate the datarow manually.
foreach (DTResult dtr in result)
{
DataRow dr = dtTable2.NewRow();
...
dr["start_time"] = Decimal.Parse(dtr.start_time.Replace(":", ""));
dr["end_time"] = Decimal.Parse(dtr.end_time.Replace(":", ""));
...
dtTable2.Rows.Add(dr);
}
And the merging code
dataTableResult.Merge(dtTable1);
dataTableResult.Merge(dtTable2);
Defintion for dtTable column:
...
dtTable2.Columns.Add("start_time", typeof(decimal));
dtTable2.Columns.Add("end_time", typeof(decimal));
...
How to solve the issue?
decimal
. There's the chance that theStart Time
field (or all fields) of those tables is of typestring
. – JimidtTable
? And its fields object type? This sorting result is quite probably coming from strings evaluation. Check twice, while debugging. – Jimi=> dataTableResult -> Rows -> Result View -> [0] -> ItemArray
, to eyeball the actual content of the rows before setting the DataSource of something? If the values are not all of the same type, you should see it there. If they're all the same object data type (decimal, here), I don't know why they shouldn't sort correctly. – Jimi