3

I am trying to do a simple sort in a DataTable but have no success. The C# code is listed but what is needed for the corrections:

using System;
using System.Data;
using System.Xml;

namespace XMLParser
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DataTable table = new DataTable();
            table.Columns.Add("code", typeof (string));

            table.Columns.Add("changePricePercentage", typeof (double));
            // Create a new XmlDocument  
            XmlDocument doc = new XmlDocument();

            // Load data  
            doc.Load(@"C:\Users\ratman\.jstock\1.0.6\Canada\watchlist\My Watchlist\realtimestock.xml");

            XmlNodeList nodes = doc.SelectNodes("//org.yccheok.jstock.engine.Stock");
            int x = 0;
            foreach (XmlElement element in nodes)
            {
                table.Rows.Add(
                    element.SelectSingleNode("code").InnerText,
                    element.SelectSingleNode("changePricePercentage").InnerText);
                Console.WriteLine(
                    "{0}: {1} {2}",
                    x,
                    element.SelectSingleNode("code").InnerText,
                    element.SelectSingleNode("changePricePercentage").InnerText);
                ++x;
            }
            Console.ReadKey();

            DataTable t = null;
            t = table.Copy();
            t.DefaultView.Sort = "[" + t.Columns[0].ColumnName + "] asc";

            //output to console for debugging
            for (int i = 0; i < t.Rows.Count; i++)
            {
                Console.WriteLine();
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    Console.Write(table.Rows[i].ItemArray[j].ToString() + "\t");
                }
            }

            Console.ReadKey();
        }
    }
}

I have looked everywhere for decent examples that actually work but found nothing.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
heavy rocker dude
  • 2,271
  • 8
  • 33
  • 47

3 Answers3

1
XmlNodeList nodes = doc.SelectNodes("//org.yccheok.jstock.engine.Stock");

// Dynamically build your data table
DataTable table = new DataTable();
foreach (XmlElement field in nodes[0].SelectNodes(".//*[not(./*)]"))
    table.Columns.Add(new DataColumn(field.Name));

// Populate with data
foreach (XmlElement element in nodes)
{
    DataRow row = table.NewRow();
    foreach (DataColumn column in table.Columns)
        row[column.ColumnName] = element.SelectSingleNode("//" + 
            column.ColumnName).InnerText;
    table.Rows.Add(row);
}

// Show sorted results
table.DefaultView.Sort = "code asc";
foreach (DataRowView row in table.DefaultView)
{
    foreach (DataColumn column in table.Columns)
        Console.WriteLine("{0}: {1}", column.ColumnName, row[column.ColumnName]);
    Console.WriteLine();
}

Console.ReadKey();

Seems you're sorting correctly, but still getting your rows from an unsorted source.

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • Thanks for posting this code. I am unsure how you mean my source is unsorted. I would imagine it would unsorted be as the XML is generated. Would that not be the point of sorting it? – heavy rocker dude Oct 20 '11 at 03:51
  • Never mind, your partial corrected what I needed which was: Console.ReadKey(); table.DefaultView.Sort = "changePricePercentage DESC"; foreach (DataRowView row in table.DefaultView) { foreach (DataColumn column in table.Columns) Console.WriteLine("{0}: {1}", column.ColumnName, row[column.ColumnName]); Console.WriteLine(); Console.ReadKey(); } – heavy rocker dude Oct 20 '11 at 04:01
0

You are sorting t but writing from table. Change Console.Write(table.Rows[i].ItemArray[j].ToString() + "\t"); to Console.Write(t.Rows[i].ItemArray[j].ToString() + "\t");

phoog
  • 42,068
  • 6
  • 79
  • 117
0

use an typed dataset create a datatable in precising type of data for example i have create a dsAppointment

  DsAppointment dsAppointmentTmp = new DsAppointment();
     DsAppointment dsAppointment = new DsAppointment();
    //add all appointment
     dsAppointmentTmp.Appointment.AddAppointmentRow(name,start,end,body)
    //use select(filter,sort(name of columns)
    DataRow[] rows1 = dsAppointmentTmp.Tables[0].Select(string.Empty, dsAppointmentTmp.Tables[0].Columns[1].ToString());

                foreach (DataRow thisRow in rows1)
                {
                        dsAppointment.Tables[0].Rows.Add(thisRow.ItemArray);

                }
    //return dsAppointment sorted
                    return dsAppointment;
Cedric Michel
  • 502
  • 5
  • 13