DataTable을 정렬하기 위해 2 줄의 코드를 만들었습니다. 이 작업을 여러 번 수행해야하므로 두 줄을 간단한 방법으로 옮겨서 정렬 할 때마다 호출해야했습니다.DataTable 정렬 방식으로 완료시 정렬 손실
그러나 DataTable을 정렬하기 위해 메서드를 호출하면 DataTable에서 정렬을 유지하지 않습니다. 내가 여기있는 곳에서 일어나는 일이 내가 깨닫지 못하고 이해할 수 없게 될 것임에 틀림 없다. 내가 그들을 원하는대로 작동
내 2 개 라인 : 위의 라인을 실행 한 후 나는 "scheduleData"의 DataTable을 볼 때
private void loadData()
{
DataTable scheduleData = new DataTable();
/*
code that fills the DataTable with data and creates the groupOrderBy string
*/
scheduleData.DefaultView.Sort = "SortDate, Finish";
scheduleData = scheduleData.DefaultView.ToTable();
scheduleData.Columns.RemoveAt(0); // at this point, when I view the
// DataTable scheduleData, the rows are sorted correctly
}
, 그것은 행이 올바른에 내가 (할 방법처럼 보인다 주문). 아래 그림과 같이
그래서, 방법로 이동하려고 :
private void loadData()
{
DataTable scheduleData = new DataTable();
/*
code that fills the DataTable with data and creates the groupOrderBy string
*/
sortDT(scheduleData, "SortDate, Finish")
scheduleData.Columns.RemoveAt(0); // at this point, when I view the
// DataTable scheduleData, the rows are no longer sorted
// (they are back to their original positions)
}
private void sortDT(DataTable dt, string sortString)
{
dt.DefaultView.Sort = sortString;
dt = dt.DefaultView.ToTable(); // at this point, when I view the
// DataTable dt, the rows are sorted correctly
}
를 메서드의 실행 중에 올바르게 행을 정렬 된 "DT"DataTable의 쇼를보기. 메서드 실행 후 "scheduleData"DataTable을 보면 정렬되지 않은 행이 표시됩니다.
여기서는 무엇을 이해하지 못하겠습니까?
"scheduleData"와 비교하여 DataTable ("dt")과 관련이 있다고 생각했습니다. 아마도 "dt"를 정렬했지만 실제로 메서드에 전달 된 "scheduleData"테이블에 영향을 미치지 않았습니다. 그래서 void가 반환되지 않는 메서드를 사용해야합니다.
그래서 내가 대신 위의 scheduleData.Columns.RemoveAt(0);
라인의이 시도 : 간단한 방법이 작동
private void loadData()
{
DataTable scheduleData = new DataTable();
/*
code that fills the DataTable with data and creates the groupOrderBy string
*/
deleteColumn(scheduleData);
}
private void deleteColumn(DataTable dt)
{
dt.Columns.RemoveAt(0);
}
그건 및 코드 실행을 호출 한 후 다시 시작하면, "scheduleData"DataTable에 그 열을했다 [0] 제거되었습니다.
분명히 C#/프로그래밍에 익숙하지 않기 때문에 매우 간단한 것이 빠져있을 수도 있습니다. DataTable을 조작하는 한 가지 방법 (열 제거)이 작동하지만 DataTable을 조작하는 다른 방법 (정렬)이 작동하지 않는 이유를 모르겠습니다.
DataTable이 참조 유형이고 변경 사항이 반영되어야하므로 void가 반환됩니다. 하지만 .ToTable()이 작동하는지 확실하지 않습니다. 이렇게하면 새 DataTable 개체가 만들어집니다. 그리고 이것은 반영되지 않을 것입니다. 속성. 테이블을 사용해보십시오. 성능면에서 훨씬 좋습니다. – Sebi