[c#] DataTable을 dataGridView에 바인딩
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
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 WindowsFormsApplication7
{
public partial class Form1 : Form
{
private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
//DataTable 생성
dt = new DataTable();
//dataGridView에 dataTable 연결
dataGridView1.DataSource = dt;
//dataGridView1에 UI에서 값을 직접 입력못하도록 함
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
}
private void button1_Click(object sender, EventArgs e)
{
//Column Add
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("AGE", typeof(int));
}
private void button2_Click(object sender, EventArgs e)
{
//Record Add
dt.Rows.Add("001", "JONAS", 30);
dt.Rows.Add("002", "KIM", 12);
dt.Rows.Add("003", "SIN", 14);
dt.Rows.Add("004", "JIK", 15);
dt.Rows.Add("005", "LIY", 19);
dt.Rows.Add("006", "RIT", 34);
dt.Rows.Add("007", "WIS", 45);
dt.Rows.Add("008", "ZIN", 84);
}
private void button3_Click(object sender, EventArgs e)
{
//1번째 row를 삭제함
dt.Rows.RemoveAt(0);
}
private void button4_Click(object sender, EventArgs e)
{
//1번째 row의 2번째 column 값을 표시함
Console.WriteLine("{0}", dt.Rows[0][1].ToString());
}
private void button5_Click(object sender, EventArgs e)
{
//1번째 row의 3번째 column 값을 변경함
dt.Rows[0][2] = 123;
}
private void button6_Click(object sender, EventArgs e)
{
//현재 선택한 셀이 포함된 row를 삭제한다
dt.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}
public Form1()
{
InitializeComponent();
}
}
}
| cs |
댓글
댓글 쓰기