728x90
1. DataSet 생성 및 DataTable 추가
DataTable dt = (treeList1.DataSource as DataTable).Copy();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
2. DataRelation 설정
// KeyFieldName이 'parentFieldName'이고 ParentFieldName이 'childFieldName'인 부모-자식 관계 설정
DataRelation relation = new DataRelation("ParentChildRelation",
ds.Tables["MASTER"].Columns["parentFieldName"], // 부모의 KeyFieldName
ds.Tables["MASTER"].Columns["childFieldName"], // 자식의 ParentFieldName
false); // 제약 조건 비활성화 (false)
ds.Relations.Add(relation); // 관계를 DataSet에 추가
3. 자식 행 유무 확인
DataRow[] dt2 = ds.Tables["MASTER"];
foreach (DataRow row in dt2)
{
// 자식 행이 있는지 확인 (관계 이름은 실제 데이터셋에 정의된 관계 이름으로 대체해야 합니다)
DataRow[] childRows = row.GetChildRows("ParentChildRelation");
if (childRows.Length > 0)
{
// 자식 행 있음
}
}
4. DataRelation 제거
ds.Relations.Remove("ParentChildRelation"); // 이름으로 제거
728x90
'C#' 카테고리의 다른 글
SOAP 통신 (0) | 2025.01.22 |
---|---|
메서드 숨기기 - new 키워드 사용 (0) | 2025.01.22 |
StackTrace 출력하기 (0) | 2024.11.13 |
[Winform] 유저 컨트롤에 속성 추가 (0) | 2024.04.25 |
Passing Output parameters to stored procedure using dapper (0) | 2024.04.24 |