概要
cscの作法、調べてみた。
練習問題、やってみた。
練習問題
datagridviewで選択したセルの値を取得せよ。
写真
サンプルコード
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;
using System.Data.OleDb;
using System.Data;
class form1: Form {
DataGridView dataGrid1;
TextBox bo1 = new TextBox();
form1() {
Text = "DataGridView";
ClientSize = new Size(500, 300);
dataGrid1 = new DataGridView();
dataGrid1.Location = new Point(50, 50);
dataGrid1.Width = 400;
ClientSize = new Size(600, 400);
bo1.Location = new Point(150, 20);
Controls.AddRange(new Control[] {
bo1,
dataGrid1
});
dataGrid1.ColumnCount = 3;
dataGrid1.Columns[0].HeaderText = "ID";
dataGrid1.Columns[1].HeaderText = "商品";
dataGrid1.Columns[2].HeaderText = "個数";
dataGrid1.Rows.Add(new Object[] {1, "みかん", 100});
dataGrid1.Rows.Add(new Object[] {2, "りんご", 300});
dataGrid1.Rows.Add(new Object[] {3, "バナナ", 200});
dataGrid1.Rows.Add(new Object[] {4, "すいか", 300});
dataGrid1.Rows.Add(new Object[] {5, "いちご", 200});
dataGrid1.Rows.Add(new Object[] {6, "メロン", 100});
dataGrid1.CellEnter += new DataGridViewCellEventHandler(dgvControl_CellEnter);
}
void dgvControl_CellEnter(object sender, DataGridViewCellEventArgs e) {
DataGridView grid = sender as DataGridView;
Console.WriteLine(grid[e.ColumnIndex, e.RowIndex].Value.ToString());
bo1.Text = grid[e.ColumnIndex, e.RowIndex].Value.ToString();
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。