Sorting in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • programmerboy
    New Member
    • Jul 2007
    • 84

    Sorting in gridview

    I am getting a set of data from this webservice http://www.webservicex.net/stockquote.asmx?op=GetQuote. It returns a string. Following is my code.

    Code:
    stockQuote = stocks.GetQuote(txtStock.Text)
    quoteDoc.LoadXml(stockQuote)
    stockNode = quoteDoc.SelectSingleNode("StockQuotes/Stock")
    
    grdStocks.DataSource = stockNode
    grdStocks.DataBind()
    grdStocks.Sort("StockFields",Sort.Ascending)
    
    Protected Sub grdStocks_Sorting(......)
    grdStocks.DataSource = stockNode
    grdStocks.DataBind()
    End Sub
    
    <asp:GridView ID="grdStocks" runat="server" >
     <Columns>
    <asp:BoundField DataField="Name" HeaderText="StockFields" />
    <asp:BoundField DataField="InnerText" HeaderText="Value" />
    </Columns>
    </asp:GridView>
    However this code doesn't get fields sorted out. How I am suppose to sort it out?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hey :)

    Sorry I couldn't answer your question sooner but here it goes.

    The GridView has a built-in sorting feature. First you have to enable the GridView to allow sorting by setting the AllowSorting property. Once you've done this the column headers will become hyperlinks and will the GridView to raise 2 events: the GridView Sorting and Sorted events.

    So your GridView should look something like:
    Code:
    <asp:GridView ID="grdStocks" runat="server"  AllowSorting="true">
    <!-- ........  -->
    </asp:GridView>
    You'll have to handle GridView Sorting Event to provide custom sorting that should be done on the data.

    The thing is that your GridView's DataSource is an XMLDocument (I think?).
    XMLDocuments do not have built-in sorting functionality.

    You're going to have to use XSL on the XML to sort it. I've never done this before and you'll probably be better off asking for help with regards to this in the XML forum.

    Or you could use different control for the GridView's DataSource. Something that does have sorting capabilities... like the DataView.

    -Frinny

    Comment

    Working...