|
■No99922 (ゆい さん) に返信
No99925 のサンプルコードを載せておきます。
Form にはデザイン画面で Button, DataGridView, BindingSource をドラッグ&ドロップして、
イベントハンドラを設定しています。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form5 : Form
{
private DataGridViewTextBoxColumn column1;
private DataGridViewTextBoxColumn column2;
private DataGridViewTextBoxColumn column3;
private DataGridViewButtonColumn column4;
public Form5()
{
InitializeComponent();
this.column1 = new DataGridViewTextBoxColumn();
this.column2 = new DataGridViewTextBoxColumn();
this.column3 = new DataGridViewTextBoxColumn();
this.column4 = new DataGridViewButtonColumn();
this.column4.UseColumnTextForButtonValue = true;
this.column4.Text = "ボタン";
this.dataGridView1.Columns.AddRange(
new DataGridViewColumn[] { this.column1,
this.column2,
this.column3,
this.column4 });
this.column1.DataPropertyName = "Name";
this.column1.HeaderText = "名前";
this.column2.DataPropertyName = "Age";
this.column2.HeaderText = "年齢";
this.column3.DataPropertyName = "Address";
this.column3.HeaderText = "住所";
this.column4.HeaderText = "詳細情報";
this.dataGridView1.DataSource = this.bindingSource1;
}
private void Form5_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var list = new List<UserInfo>
{
new UserInfo{ Name="Mike", Age=25, Address="Maryland" },
new UserInfo{ Name="John", Age=30, Address="Virginia" },
new UserInfo{ Name="Tom", Age=23, Address="West Virginia" }
};
this.bindingSource1.DataSource = list;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int index = e.RowIndex;
// 押下された行の各項目を別のテキストボックスに書き出す
}
}
public class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
}
|