'DataView.Sort not working despite using datatable

I have a dataTable, which then I stored into the DataView for sorting purposes. The sorting is custom based on the fieldname.

First, I add additional column using

dt.Columns.Add("Sorting", typeof(int));

After that, I run foreach loop and check using switch to determine the sorting number for each row in data table. For example, inside column named "Priority", there are 4 different choices either "Extremely High", "High", "Medium", "Low".

foreach (DataRow dr in dt.Rows)
{
    string Priority = dr["Priority"].ToString();
    switch (Priority)
    {
            case "Extremely High": dr["Sorting"] = 1; break;
            case "High": dr["Sorting"] = 2; break;
            case "Medium": dr["Sorting"] = 3; break;
            case "Low": dr["Sorting"] = 4; break;
    }
    dt.AcceptChanges();
    dr.SetModified();
}

So, I tried to sort using DataSort, but it isn't working in the chart. This is my code:

DataView dv = dt.DefaultView;
dv.Sort = "Sorting";
dt = dv.ToTable();

ChartStatus.DataSource = dt;
ChartStatus.DataBindCrossTable(dt.DefaultView, "Status", "Priority", "value", "");
ChartStatus.DataManipulator.GroupByAxisLabel("SUM", "*");
ChartStatus.AlignDataPointsByAxisLabel();

The reason I want to sort the data before binding because I want the chart to display the value with higher priority first. Let say my bar chart has 4 segments in y-axis. I want that axis to sort by priority, rather than sort alphabetically.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source