2011-09-06 7 views
3

나는 C#으로 데이터 테이블을 가지고 있습니다.ADO.NET 데이터 테이블 정렬

DataTable dtClientMedications = new DataTable(); 
dtClientMedications.Columns.Add("Id"); 
dtClientMedications.Columns.Add("MedId"); 
dtClientMedications.Columns.Add("BrandName"); 
dtClientMedications.Columns.Add("GenericName"); 
dtClientMedications.Columns.Add("Type"); 
dtClientMedications.Columns.Add("Dosage"); 
dtClientMedications.Columns.Add("Status"); 
dtClientMedications.Columns.Add("SortOrder"); 

는 내가있는 gridview에 할당 열 SORTORDER &에 의해 정렬합니다. 나는 이것을 사용했다 :

gdvMainList.DataSource = dtClientMedications.DefaultView.Sort[7];//dtClientMedications; 
gdvMainList.DataBind(); 

그러나 인덱스는 경계 밖의 예외를 제공한다.

+0

데이터 테이블 열의 길이에 따라 다릅니다. 데이터 테이블에 몇 개의 열이 포함되어 있습니까? –

+0

코드를 제공 한 것처럼 8 개의 열이 있습니다. – asma

답변

3

: 당신은 테이블 자체의 열을 정렬 할 수 있습니다

dtClientMedications.DefaultView.Sort="ID" 
+0

좋아요, 데이터 뷰를 데이터 소스로 GridView에 할당 할 수 있습니까? 나는 데이터 테이블로하는 것처럼. – asma

+0

예. 나도 그렇게 생각해. 나는 이것을 잠시 동안하지는 않았지만 어쨌든 "기본"보기이기 때문에 둘 중 하나를 지정하는 것이 비슷할 것이라고 생각합니다. 대답에서 업데이트 된 코드를 참조하십시오. – Meligy

+0

좋아, 고맙습니다. 이걸 시험해 봅시다. – asma

1

Sort은 문자열 속성입니다. 당신은 Sort 속성이 실제로 무엇인지 누락

dtClientMedications.Columns["SortOrder"].SetOrdinal(0); 
+0

제 경우에는'dtClientMedications.DefaultView.Sort = "SortOrder"'??? – asma

+0

데이터 소스가 Sort 메서드를 지원해야합니까? – McArthey

+0

포인트를 얻지 못했습니다 – asma

1

사용.

정렬하고 싶은 표현입니다. A 값이며 열의 인덱스는 아닙니다. 귀하의 코드는 가정 된 기존 정렬 string에서 여섯 번째 char을 읽으려고합니다.

데이터 바인딩하기 전에 사용

dtClientMedications.DefaultView.Sort = "SortOrder"; 

.

gdvMainList.DataSource = dtClientMedications.DefaultView; // You may not need to mention view 
gdvMainList.DataBind(); 

.

문서 : http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

+0

사실 모든 열을 SortOrder로 전체 정렬 할 수 있습니다. – asma