1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
private void button2_Click(object sender, EventArgs e)
{
string[] name = new string[] { "もちふわパンケーキ", "あんぱん", "チョココロネ", "メロンパン" };
int[] value = new int[] { 100, 100, 110, 120 };
DataGridViewColumn colA = CreateDataGridViewTextBoxColumn("ID", "ID", 60, typeof(string));
DataGridViewColumn colB = CreateDataGridViewTextBoxColumn("Name", "Name", 120, typeof(string));
DataGridViewColumn colC = CreateDataGridViewTextBoxColumn("Value", "Value", 60, typeof(int));
dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
dataGridView1.Columns.Add(colA);
dataGridView1.Columns.Add(colB);
dataGridView1.Columns.Add(colC);
for (int i = 0; i < 4; i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["ID"].Value = i.ToString("0000");
dataGridView1.Rows[i].Cells["Name"].Value = name[i];
dataGridView1.Rows[i].Cells["Value"].Value = value[i];
}
}
/// <summary>
/// DataGridViewのColumn型を作成します。
/// </summary>
/// <param name="name">列の名前</param>
/// <param name="header">列のヘッダー</param>
/// <param name="width">列の現在の幅</param>
/// <param name="type">列のセルの値のデータ型</param>
/// <returns>DataGridViewTextBoxColumnオブジェクト</returns>
private DataGridViewColumn CreateDataGridViewTextBoxColumn(string name, string header, int width, Type type)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = name;
col.DataPropertyName = name;
col.HeaderText = header;
col.ValueType = type;
col.Width = width;
return col;
}
|