1. 칼럼 생성 및 DataDouce와 연동되는 특성 설정하기
DataSource에 연결할 때 사용되는 데이터의 컬럼명을 DataPropertyName에 입력한다.
예를들면 데이터베이스에서 로딩한 데이터를 DataTable로 수신했을 때
그 수신된 데이터그룹의 칼럼명을 DataPropertyName에 입력하면 된다.
2. 예제
Random rnd = new Random();
DataTable table = new DataTable();
DataColumn col;
col = table.Columns.Add("CHK", typeof(bool));
col.ReadOnly = false;
col = table.Columns.Add("NAME", typeof(string));
col.ReadOnly = true;
col = table.Columns.Add("AGE", typeof(int));
col.ReadOnly = true;
col = table.Columns.Add("SEX", typeof(string));
col.ReadOnly = true;
for (int i = 0; i < 100; ++i)
{
DataRow row = table.NewRow();
row.ItemArray = new object[]
{
((rnd.Next(1, 1000) % 2) == 0),
$"Name{(i + 1)}",
rnd.Next(1, 100),
((rnd.Next(1, 1000) % 2) == 0 ? "남" : "여")
};
table.Rows.Add(row);
}
grdView.DataSource = table;
위의 예제는 DataGridView의 칼럼 생성시 DataPropertyName을
첫번째 칼럼은 CHK, 이름은 NAME, 나이는 AGE, 성별은 SEX로 했고,
DataTable의 칼럼은 각각 CHK, NAME, AGE, SEX로 되어 있다.
'유용한 정보' 카테고리의 다른 글
[C#] 사용자 정의 달력 (0) | 2025.05.14 |
---|---|
[clr] Window Forms 컨트롤 라이브러리 MFC에서 사용하기 사용 (0) | 2025.05.06 |
[C#] DataGridView 자동 행 번호 생성하는 방법(예제) (0) | 2025.05.05 |
[C#] 사용자 정의 이벤트 등록 및 사용 예제 (0) | 2025.04.27 |
[C#] MonthCalendar 요일 클릭하기 (0) | 2025.04.24 |