C# – How to get sorted data back from a DataBound gridview


Here's the scenario I am trying to achieve. I have a DataGrid control on my form. I am using XCeed Data Grid for .Net as my control of choice. I am driving the DataGrid via a DataTable as its DataSource. The Grid allows me to sort the data by column which is very cool. That same Data is also being plotted on a Chart control. This way you can see the data as a table AND a chart.

What I want to know is to be able to sort the data in the grid by clicking on a column and then see the new sorted data updated on the chart. Generally speaking, how would you go about doing something like that? How do you grab sorted data back from a gridview contorl? Extra points for telling me how it's done with XCeed Grid control 🙂


Best Solution

do the following on the sort event for the grid.. or whenever

        //get data w/ sort expression
        DataTable dt = (DataTable)gridview1.DataSource;

        //bind sorted data to another control
        chartcontrol.datasource = dt.DefaultView.ToTable();

        //add sorted ids to session to get crazy with
        if (dt != null)
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
                sb.Append(dt.DefaultView[i]["ID"].ToString() + ",");
            if (sb.Length > 0)
                sb.Remove(sb.Length - 1, 1);

            Session["SortedIDs"] = sb.ToString();