DataGridViewやListBox、Button等はすべてデザイナーで配置した
必要なイベントはコード内に書いた
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DataBindingTest4
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
public Form1()
{
InitializeComponent();
dt1.TableName = "住所録";
dt1.Columns.Add("Name", typeof(string));
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("Adress", typeof(string));
dt1.Rows.Add(new object[] { "test1", 12,"Osaka" });
dt1.Rows.Add(new object[] { "test2", 24,"Tokyo" });
dt2.TableName = "メニュー";
dt2.Columns.Add("Menu", typeof(string));
dt2.Columns.Add("value", typeof(int));
dt2.Columns.Add("TaxValue", typeof(int),"value+value*0.05");
dt2.Rows.Add(new object[] { "チャーハン", 500 });
dt2.Rows.Add(new object[] { "ラーメン", 650 });
dt3.TableName = "ファイル履歴";
dt3.Columns.Add("File", typeof(string));
dt3.Rows.Add(new object[] { @"c:\test.jpg" });
dt3.Rows.Add(new object[] { @"c:\test2.jpg" });
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
ds.Tables.Add(dt3);
//リストボックスコントロールのセレクタ用にDataSetのDataTableの名前を抜き出している
List<string> tableList = new List<string>();
foreach (DataTable item in ds.Tables)
{
tableList.Add(item.TableName);
}
dataGridView1.DataSource = ds; //DataGridViewのデーターソースとしてDataSetを利用している(ちなみにDataGridViewはDataGridの上位互換クラス)
listBox1.DataSource = tableList; //初期表示
dataGridView1.DataMember = (string)listBox1.SelectedItem; //DataSetに含まれるDataTableを表示するデータメンバーとして登録
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
}
void button2_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
ds.Clear();
ds.ReadXml(myStream);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
void button1_Click(object sender, EventArgs e)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Insert code to read the stream here.
ds.WriteXml(myStream);
myStream.Close();
}
}
}
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataGridView1.DataMember = (string)listBox1.SelectedItem; //インデックスを変えるたびDataGridViewの表示を変更している
}
}
}
最終更新:2012年09月05日 23:48