?
Solved

Sorting in GridView with xml datasource

Posted on 2007-10-14
3
Medium Priority
?
4,559 Views
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.

Thanks

Wing


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

Expert Comment

by:bullrout
ID: 20076562
HI There,

Lots of information is available on this subject.

http://www.satheeshbabu.com/Articles/ASPNET/DataSourceControls.aspx

or

One possible solution:


System.Data.
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;

gridView1.DataBind();


0
 
LVL 1

Author Comment

by:WingYip
ID: 20077553
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.
0
 
LVL 5

Accepted Solution

by:
bullrout earned 150 total points
ID: 20082660
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;
            gv.DataBind();
      }
}
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

757 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