2013-10-15 78 views
0

我有一個Gridview名爲SearchGenericReport。我想要在Gridview Columns的標題中對圖像進行上下排序。但GetSortColumnIndex(string strrCol)總是返回- value。所以我不能添加圖像。我在這裏錯過了什麼?在gridview標題中排序圖像

public int GetSortColumnIndex(String strCol) 
{ 
    DataTable result= Session["TaskTable"] as DataTable; 
    foreach (DataControlField field in result.Columns) 
    { 
     if (field.SortExpression == strCol) 
     { 
      return SearchGenericReport.Columns.IndexOf(field); 
     } 
    } 

    return -1; 
} 
void AddSortImage(GridViewRow headerRow) 
{ 

    int selCol = GetSortColumnIndex(m_strSortExp); 

    //if (-1 == selCol) 
    //{ 
    // return; 
    //} 

    // Create the sorting image based on the sort direction 
    Image sortImage = new Image(); 

    if (SortDirection.Ascending == m_SortDirection) 
    { 
     sortImage.ImageUrl = "img/uparrow.png"; 
     sortImage.AlternateText = "Ascending"; 
    } 
    else 
    { 
     sortImage.ImageUrl = "img/downarrow.png"; 
     sortImage.AlternateText = "Descending"; 
    } 

    // Add the image to the appropriate header cell 
    headerRow.Cells[selCol].Controls.Add(sortImage); 

} 

但是,當我快速監視在gridview.Columns - 它在 「enumertaion沒有結果」 的結果和一些東西。 及其多年平均值進入For Each循環僅..我已經修改這樣的代碼 - INT GetSortColumnIndex(){

 // Iterate through the Columns collection to determine the index 
     // of the column being sorted. 
     foreach (DataControlField field in this.SearchGenericReport.Columns) 
     { 
      if (field.SortExpression == SearchGenericReport.SortExpression) 
      { 
       return SearchGenericReport.Columns.IndexOf(field); 
      } 
     } 

     return -1; 
    } 
    void AddSortImage(int columnIndex,GridViewRow headerRow) 
    { 
     // Create the sorting image based on the sort direction 
     Image sortImage = new Image(); 
     SortDirection direction = SearchGenericReport.SortDirection; 
     if (direction == SortDirection.Ascending) 
     { 
      sortImage.ImageUrl = "img/uparrow.png"; 
      sortImage.AlternateText = "Ascending"; 
     } 
     else 
     { 
      sortImage.ImageUrl = "img/downarrow.png"; 
      sortImage.AlternateText = "Descending"; 
     } 

     // Add the image to the appropriate header cell 
     headerRow.Cells[columnIndex].Controls.Add(sortImage); 
    } 

回答

0

當多個列進行排序,字段。 SortExpression屬性包含用於排序的字段的逗號分隔列表。所以你可以嘗試使用Contains(strCol)

更好的解決方案是分割逗號分隔的字符串表達式並檢查它是否有列strCol