[ C#-DataTable : Column 목록 얻기 ]

 

1. 정의

    - DataTable에 속해 있는 컬럼목록을 String[] 에 Setting하기

 

    * DataTable객체에서 컬럼목록을 String 배열로 얻기가 뭐 이리 복잡한지... 문법 의미를 모르겠네 ㅋ

 

2. 방법1 - LINQ 메서드 이용

    -  String[] colNm = datatable.Columns.Cast<DataColumn >()
                                                                .Select(x => x.ColumnName)
                                                                .ToArray();

 

3. 방법2 - LINQ 쿼리 이용

    -  String[] colNm2 = (from dc in datatable.Columns.Cast<DataColumn >()
                                                                                     select dc.ColumnName).ToArray();

 

4. 방법3

    - var arrayNames = (from DataColumn x in datatable.Columns
                                                                                       select x.ColumnName).ToArray();

 

 

5. 방법4

    - DataTable dt = new DataTable();
       foreach (DataColumn dc in datatable.Rows.Columns)
       {
                if (dc.ColumnName == "MyColumn")
                {
                    // Do code
                }
       }

 

6. 방법5

    - // For each DataTable, print the ColumnName.
      foreach (DataTable table in dataSet.Tables)
      {
          
foreach (DataColumn column in table.Columns)
           
{
              
Console.WriteLine(column.ColumnName);
          
}
      }

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

C#-DataTable : Column 목록 얻기  (0) 2018.08.17
C#-DataTable : 기본  (0) 2018.08.17
Posted by 농부지기