(C#) DataSetをソートする

System.Data.DataViewを使う。

ソースコード

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Sample
{
    public class SortedDataSet : Form
    {
        private DataSet ds;
        private DataView view;

        private TextBox edit;
        private Button fillButton;
        private Button ascButton;
        private Button descButton;

        private bool filled = false;

        public SortedDataSet()
        {
            edit = new TextBox();
            edit.Location = new Point(10, 10);
            edit.Size = new Size(200, 50);

            fillButton = new Button();
            fillButton.Text = "Fill";
            fillButton.Location = new Point(10, 40);
            fillButton.Size = new Size(50, 20);
            fillButton.Click += new EventHandler(Fill_Click);

            ascButton = new Button();
            ascButton.Text = "SortASC";
            ascButton.Location = new Point(70, 40);
            ascButton.Size = new Size(60, 20);
            ascButton.Enabled = false;
            ascButton.Click += new EventHandler(SortASC_Click);

            descButton = new Button();
            descButton.Text = "SortDESC";
            descButton.Location = new Point(140, 40);
            descButton.Size = new Size(70, 20);
            descButton.Enabled = false;
            descButton.Click += new EventHandler(SortDESC_Click);

            this.Text = "SortedDataSet";
            this.ClientSize = new Size(300, 140);
            this.Controls.Add(edit);
            this.Controls.Add(fillButton);
            this.Controls.Add(ascButton);
            this.Controls.Add(descButton);
        }

        private void Fill_Click(object sender, EventArgs e)
        {
            ds = new DataSet();

            DataTable table = ds.Tables.Add("NumberTable");
            table.Columns.Add("number", typeof(int));

            for (int i = 0; i < 10; i++)
            {
                DataRow row = table.NewRow();
                row["number"] = i;

                table.Rows.Add(row);
            }

            PrintDataSet();
            Filled = true;
        }

        private void SortASC_Click(object sender, EventArgs e)
        {
            view = new DataView(ds.Tables["NumberTable"]);
            view.Sort = "number ASC";

            PrintView();
        }

        private void SortDESC_Click(object sender, EventArgs e)
        {
            view = new DataView(ds.Tables["NumberTable"]);
            view.Sort = "number DESC";

            PrintView();
        }

        private void PrintDataSet()
        {
            ArrayList numbers = new ArrayList();

            foreach (DataRow row in ds.Tables["NumberTable"].Rows)
            {
                numbers.Add(row["number"].ToString());
            }

            edit.Text = String.Join(" ", (string[])numbers.ToArray(typeof(string)));
        }

        private void PrintView()
        {
            ArrayList numbers = new ArrayList();

            foreach (DataRowView row in view)
            {
                numbers.Add(row["number"].ToString());
            }

            edit.Text = String.Join(" ", (string[])numbers.ToArray(typeof(string)));
        }

        private bool Filled
        {
            get
            {
                return filled;
            }
            set
            {
                if (value)
                {
                    ascButton.Enabled = true;
                    descButton.Enabled = true;
                }

                filled = value;
            }
        }

        public static void Main()
        {
            Application.Run(new SortedDataSet());
        }
    }
}
    

関連情報

Copyright © 2005, AfternoonTea
All rights reserved.