2011-03-30 8 views
2

프로그램은 정상적으로 실행되지만 오름차순 및 내림차순 단추는 아무 것도 처리하지 않습니다. 테이블의 모든 데이터가 포함 된 DataGridView는 동일하게 보이며 정렬되지 않습니다. 제목별로 정렬한다고 가정합니다. 어쩌면 정렬하지만 DataGridView를 새로 고치지 않습니다?DataView는 DataGridView에서 오름차순 또는 내림차순 정렬하지 않습니다.

private void btnSortAscendingRecords_Click(object sender, EventArgs e) 
     { 
      DataView TitlesDataView = new DataView(booksDataset1.Books); 

      TitlesDataView.Sort = "BookTitle ASC"; 

      //sort asc titles in videosgrid 

     } 

     private void btnSortDescendingRecords_Click(object sender, EventArgs e) 
     { 
      DataView TitlesDataView = new DataView(booksDataset1.Books); 

      TitlesDataView.Sort = "BookTitle DESC"; 

      //sort descending titles in videosgrid 
     } 

답변

1

방금 ​​만든 새 DataView에 데이터 소스를 설정해야합니다. 나는 이것이 창문 양식 응용 프로그램이라고 생각합니까?

그럼 경우 :

[YourDataGridView].DataSource = TitlesDataView; 
+0

내가 찾고있는 것. – HelloWorld

+0

@HelloWorld 쿨, 기쁘게 도왔습니다. –

0

DataGridView에이는 DataView를에 참조하지만 모든 경우에 데이터 소스 (DataView를, BindingSource에, 표, 데이터 집합 + "TABLENAME")에 결합한다. 이 DataView를 참조를 얻고 당신이 원하는대로 정렬 (및 필터)을 설정합니다

DataView dv = null; 
CurrencyManager cm = (CurrencyManager)(dgv.BindingContext[dgv.DataSource, dgv.DataMember]); 

if (cm.List is BindingSource) 
{ 
    // In case of BindingSource it may be chain of BindingSources+relations 
    BindingSource bs = (BindingSource)cm.List; 
    while (bs.List is BindingSource) 
    { bs = bs.List as BindingSource; } 

    if (bs.List is DataView) 
    { dv = bs.List as DataView; } 
} 
else if (cm.List is DataView) 
{ 
    // dgv bind to the DataView, Table or DataSet+"tablename" 
    dv = cm.List as DataView; 
} 

if (dv != null) 
{ 
    dv.Sort = "somedate desc, firstname"; 
    // dv.Filter = "lastname = 'Smith' OR lastname = 'Doe'"; 

    // You can Set the Glyphs something like this: 
    int somedateColIdx = 5; // somedate 
    int firstnameColIdx = 3; // firstname 
    dgv.Columns[somedateColIdx].HeaderCell.SortGlyphDirection = SortOrder.Descending; 
    dgv.Columns[firstnameColIdx].HeaderCell.SortGlyphDirection = SortOrder.Ascending; 
} 

참고 : 정렬 및 필터에 사용 열 이름을 열 이름에, DataTable을의 열 이름에 해당 DataGridView는 dgv에 셀을 표시하는 데 사용되는 컨트롤의 이름입니다. 이 같은 DataView를에 사용되는 열 이름을 얻을 수 있습니다 :

당신이 분류 열을 추적 할 어떻게의
string colName = dgv.Columns[colIdx].DataPropertyName 

다릅니다 (colSequence, colName 사용, ASC/DESC, dgvColIdx) 당신은 정렬 구축과 표현을 필터링하는 방법을 결정할 수 있습니다 dgv에서 SortGlyph를 설정합니다 (간단히하기 위해 하드 코드를 만들었습니다).

관련 문제