对网格视图标题中的图像进行排序

本文关键字:图像 排序 网格 视图 标题 | 更新日期: 2023-09-27 18:35:37

我有一个名为SearchGenericReportGridview。我想在网格视图列的标题中对图像进行排序。但是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);
}

但是当我在网格视图上快速观看时。列 - 它在"枚举不产生任何结果"之类的结果中。并且它不会只进入每个循环。我已经修改了这样的代码 -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);
    }

对网格视图标题中的图像进行排序

对多列进行排序时,字段。属性包含要作为排序依据的字段的逗号分隔列表。所以你可以尝试使用Contains(strCol) .

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