| ■No87770 (いまそかり さん) に返信
C# ですがサンプルを書いておきます。Form5 ⇒ Form1, Form6 ⇒ Form2 に読み替えてください。
デザイナで Form5 には Button を、Form6 には ComboBox をドラッグ&ドロップして、必要なイ
ベントハンドラを生成しただけです。
Form5.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ID", typeof(Int32)));
table.Columns.Add(new DataColumn("Name", typeof(string)));
for (int i = 0; i < 5; i++)
{
DataRow row = table.NewRow();
row["ID"] = i;
row["Name"] = "Product Name_" + i.ToString();
table.Rows.Add(row);
}
DataSet df = new DataSet();
df.Tables.Add(table);
Form6 frm = new Form6(df);
frm.Show();
}
}
}
Form6.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form6 : Form
{
private DataSet df;
public Form6()
{
InitializeComponent();
}
public Form6(DataSet df)
{
InitializeComponent();
this.df = df;
}
private void Form6_Load(object sender, EventArgs e)
{
comboBox1.DataSource = df.Tables[0];
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ID";
}
}
} |