0

我有一个Gridview名为SearchGenericReport. 我想在 Gridview Columns 的标题中对图像进行上下排序。但GetSortColumnIndex(string strrCol)总是回报- value。所以我不能添加图像。我在这里想念什么?

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 上快速观看时 - 它的结果是“枚举没有结果”等等。而且它不会只进入 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);
    }
4

1 回答 1

0

当对多列进行排序时,字段。SortExpression属性包含一个以逗号分隔的字段列表,用于排序。因此,您可以尝试使用Contains(strCol).

更好的解决方案是拆分逗号分隔的字符串表达式并检查它是否有列strCol

于 2013-10-15T11:02:33.380 回答