C# DataGridView ComboBox 넣기 예제
C# DataGridView ComboBox 넣기 예제
IDE tool : Microsoft Visual Studio 2005
빈 화면에 DataGridView를 하나 올리고 버튼을 하나 추가.
버튼을 누를때 마다 로우를 추가하면서 콤보박스 구성
// 소스
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataGridViewComboBoxExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//ComboBox 유형의 셀 만들고
DataGridViewComboBoxCell cCell = new DataGridViewComboBoxCell();
//DisplayStyle을 ComboBox로 설정
cCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
//ComboBox에 데이터 임시로 넣고
cCell.Items.Add("A");
cCell.Items.Add("B");
cCell.Items.Add("C");
//데이터그리뷰에 로우를 추가하면서 콤보박스 자리는 null로 셋팅
int nRow = dataGridView1.Rows.Add("1", "2", null);
//추가한 로우 위치에서 콤보박스 셀 자리에 생성한 콤보박스 할당
dataGridView1.Rows[nRow].Cells[2] = cCell;
}
}
}