developer tip

위치 0에 C # 데이터 테이블 삽입 열

optionbox 2020. 8. 20. 08:12
반응형

위치 0에 C # 데이터 테이블 삽입 열


누구든지 위치 0의 데이터 테이블에 열을 삽입하는 가장 좋은 방법을 알고 있습니까?


다음 코드를 사용하여 위치 0에서 Datatable에 열을 추가 할 수 있습니다.

    DataColumn Col   = datatable.Columns.Add("Column Name", System.Type.GetType("System.Boolean"));
    Col.SetOrdinal(0);// to put the column in position 0;

Wael의 답변을 개선하고 한 줄에 넣으려면 :

dt.Columns.Add("Better", typeof(Boolean)).SetOrdinal(0);

업데이트 : 이것은 DataColumn으로 다른 작업을 수행 할 필요가 없을 때 작동합니다. Add ()는 해당 열을 반환하고 SetOrdinal ()은 아무것도 반환하지 않습니다.


    //Example to define how to do :

    DataTable dt = new DataTable();   

    dt.Columns.Add("ID");
    dt.Columns.Add("FirstName");
    dt.Columns.Add("LastName");
    dt.Columns.Add("Address");
    dt.Columns.Add("City");
           //  The table structure is:
            //ID    FirstName   LastName    Address     City

       //Now we want to add a PhoneNo column after the LastName column. For this we use the                               
             //SetOrdinal function, as iin:
        dt.Columns.Add("PhoneNo").SetOrdinal(3);

            //3 is the position number and positions start from 0.`enter code here`

               //Now the table structure will be:
              // ID      FirstName   LastName    LastName   PhoneNo     Address     City

참고 URL : https://stackoverflow.com/questions/1339880/c-sharp-datatable-insert-column-at-position-0

반응형