Sorting in GridView with xml datasource

Posted on 2007-10-14
Last Modified: 2008-10-22
Anyone know I can I sort a gridview by a specific column?  Basically I want to just open the gridview sorted by course_title bu the xml source is not sorted and the gridview displays in the same order

The following gridview is based on an xml datasource.



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1" CssClass="grid_background" GridLines="None">
                    <asp:BoundField DataField="course_id" HeaderText="Course ID">
                        <ItemStyle Width="100px" />
                        <HeaderStyle HorizontalAlign="Left" />
                    <asp:TemplateField HeaderText="Course Title" SortExpression="course_title">
                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                             <asp:Label ID="Label1" runat="server" Text='<%# XPath("course_title")%>'></asp:Label>
                        <ItemStyle Width="240px" />
                        <HeaderStyle HorizontalAlign="Left" />
                    <asp:TemplateField HeaderText="Duration">
                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                             <asp:Label ID="Label1" runat="server" Text='<%# XPath("course_duration")%>'></asp:Label>
                        <ItemStyle Width="100px" />
                        <HeaderStyle HorizontalAlign="Left" />
                <RowStyle CssClass="grid_row" />
                <SelectedRowStyle CssClass="selected_row" />
                <HeaderStyle CssClass="grid_header" />
                <AlternatingRowStyle CssClass="grid_alternate_row" />
Question by:WingYip
    LVL 5

    Expert Comment

    HI There,

    Lots of information is available on this subject.


    One possible solution:

    DataView dv = new System.Data.DataView(MyDataSet.Tables[0]);

    Then sort the DataView: -

    dv.Sort = "mycol DESC"

    Then bind the GridView to the DataView: -

    gridView1.DataSource = dv;


    LVL 1

    Author Comment

    Yes I understand but woudn't that be more efficient with sql data?

    How could I bind an xml file to a dataset?

    I have actually sorted (as it were) my problem now but am interested in seeing this other approach.

    To sort things out I had to write an xsl file that reformats the xml file into another sorted xml file before the gridview binds to it.
    LVL 5

    Accepted Solution

    HI There,

    binding to a dataset:

    public partial class _Default : System.Web.UI.Page
          protected void MyGridView_PreRender(object sender, EventArgs e)
                // Creates a DataSet and loads it with an Xml Content
                DataSet aDataSet = new DataSet();
                aDataSet.ReadXml(new StringReader(aXmlDoc.OuterXml));

                // Bind the DataSet to the grid view
                GridView gv = (GridView)sender;
                gv.DataSource = aDataSet;

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now