どっとねっとふぁん

C# Tips And Samples
in 検索

16.DataGridを使う

最新の投稿は、投稿日時: 06-24-2005, 11:22 午前 投稿者: ono です。スレッドには 0 件の返答があります。
投稿のソート: 前へ 次へ
  •  06-24-2005, 11:22 午前 88

    16.DataGridを使う

    DataSetに格納したデータを扱うにはDataGridを利用するのが便利です。
    WindowsアプリケーションではDataSetとDataGridを関係付けるだけで、データの追加/更新/削除が可能になります。
    以下のサンプルでConsoleアプリケーションTips 17で作成したtestdata1.xmlを利用してDataGridの動作を確認することができます。
    -------------------------------------------------------
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Data;
    
    class WinSample : Form
    {
        DataGrid dg;
        DataSet ds;
    
        public static void Main()
        {
            Application.Run(new WinSample());
        }
    
        public WinSample()
        {
            this.SuspendLayout();
    
            // DataGridの生成
            dg = new DataGrid();
            dg.Anchor = AnchorStyles.Top|AnchorStyles.Bottom|AnchorStyles.Left|AnchorStyles.Right;
            dg.Location = new Point(10, 10);
            dg.Size = new Size(275, 100);
            this.Controls.Add(dg);
    
            // DataSetの生成
            ds = new DataSet();
    
            this.Text = "DataGrid Sample";
            this.Size = new Size(300, 200);
    
            Button mybutton = new Button();
            mybutton.Anchor = AnchorStyles.Bottom|AnchorStyles.Right;
            mybutton.Location = new Point(200, 130);
            mybutton.Text = "保存";
            mybutton.Click += new EventHandler(this.mb_Click);
            this.Controls.Add(mybutton);
    
            this.ResumeLayout();
        }
    
        protected override void OnLoad(EventArgs e)
        {
            // データの読込み
            ds.ReadXml("testdata1.xml");
            dg.DataSource = ds.Tables[0];
        }
    
        private void mb_Click(object sender, EventArgs e)
        {
            // データの保存
            ds.WriteXml("testdata2.xml", XmlWriteMode.WriteSchema);
        }
    }
    
    -------------------------------------------------------
    DataGridは表示されているデータのソートもできますし、データ入力時に型が合わないデータは受け付けないなど、これだけで簡単なデータ操作アプリケーションができてしまいます。
    ちょっと手を加えることでもっと便利になるので、そのあたりについてこれから少しずつまとめていこうと思います。


    あおい情報システム株式会社
     小野修司(どっとねっとふぁん)
RSS ニュースフィードで参照
SkinName:iroha_CS2
Powered by Community Server, by Telligent Systems