lilybetty 님의 블로그

C# DataGridView 행 추가 방법 본문

C#

C# DataGridView 행 추가 방법

lilybetty 2025. 3. 7. 23:27

DataGridView는 Windows Forms 애플리케이션에서 데이터를 표 형태로 표시하고 편집할 수 있도록 해주는 강력한 컨트롤입니다. 이번 포스트에서는 DataGridView에 행을 추가하는 방법을 단계별로 살펴보겠습니다.

 

1. DataGridView 설정

먼저, Form에 DataGridView 컨트롤을 추가합니다.

  • 디자이너에서 추가:
    1. Visual Studio에서 Form 디자인 화면으로 이동합니다.
    2. 도구 상자에서 DataGridView를 드래그하여 폼에 추가합니다.
    3. 이름은 dataGridView1로 설정합니다.
  • 컬럼 추가:
// 컬럼 수동 추가 예시
dataGridView1.Columns.Add("Column1", "이름");
dataGridView1.Columns.Add("Column2", "나이");
dataGridView1.Columns.Add("Column3", "직업");
 

2. 행 추가 방법

코드로 행 추가:

// 데이터 행 추가 예시
string[] row = { "홍길동", "30", "개발자" };
dataGridView1.Rows.Add(row);

// 직접 Cell 값 설정
int rowIndex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIndex].Cells[0].Value = "김철수";
dataGridView1.Rows[rowIndex].Cells[1].Value = "28";
dataGridView1.Rows[rowIndex].Cells[2].Value = "디자이너";

데이터 행 추가한 코드로 홍길동이 추가되었고

직접 Cell 값 설정한 코드로 김철수가 추가된 것을 확인할 수 있습니다.

코드로 행 추가한 실행결과

버튼 클릭 시 행 추가:

private void btnAddRow_Click(object sender, EventArgs e)
{
    dataGridView1.Rows.Add("이름", "나이", "직업");
}

이름, 나이, 직업 행이 추가된 것을 확인할 수 있습니다.

버튼 클릭 시 실행 결과

'C#' 카테고리의 다른 글

C# DataGridView에 CheckBox 추가  (1) 2025.03.16
C# DataGridView간 이동 (DataGridView간 데이터 옮기기)  (0) 2025.03.15
C# DataGridView 행 삭제 방법  (0) 2025.03.08