基于行级C#对ASP.NET GridView控件进行排序

开发中心

我的问题与之前有关stackoverflow的问题有关。在我之前的问题中,我需要在ac#asp.net Gridview Control中合并具有相同值的列。其他stackoverflowers的出色帮助和支持解决了此问题,但是我还需要按行级别对顺序进行排序,例如:

当前格式: 在此处输入图片说明

gridview控件允许我按列(从上到下)对值进行排序,但是如果您查看(当前格式)屏幕截图并检查行,则可以使用一种方法对行进行排序,例如:在行级别上从左到右3,您将看到以下结果:(2a,2c,2x2a,2c)现在,理想情况下,我想将它们分组为(3x2a,2x2c),但是不确定这是否是正确的方法,因为它将涉及到移位我可以想象,如果我们正确地对第一行进行排序并转到第二行,我们可能会再次弄乱第一行的结果,不确定这是否可行甚至可能。

我想让我的gridview控件按如下所示显示数据:

所需格式: 在此处输入图片说明

  1. 这是asp.net gridview控件中的方法还是可以实现的?
  2. 否则我将如何实现理想的解决方案?

如果您需要任何其他信息,请询问,我将尝试扩大我的问题。

更新信息:

ConnersFan的建议之后,我得到了以下结果,这个想法有点儿存在,但还不完全是。

运行页面后,我得到此屏幕截图,在每个屏幕截图之后,我将解释发生的情况:

在此处输入图片说明^^^ DEFAULT PAGE LOAD:这会将行单元格中的相等值合并/分组在一起。

在此处输入图片说明^^^单击第一行“排序”链接后,第一行看起来正确,列标题和值都移到了正确的位置。

在此处输入图片说明^^^单击第二行的“排序”链接后,它将正确地对第二行进行排序,并且没有弄乱第一行

在此处输入图片说明 ^^^ After clicking on the third row 'sort' link, it messes up the first and second row, but managed to merge and group the third row ok.

在此处输入图片说明 ^^^ After clicking on the fourth row 'sort' link, it messes up the first, second and third row, but managed to merge and group the fourth row ok.

在此处输入图片说明 ^^^ After clicking on the fifth row 'sort' link, (which should not make a difference as they are all the same equal value, it messes up the first, second, third and fourth row, but managed to merge and group the fifth row still ok.

在此处输入图片说明 ^^^ After clicking on the sixth row 'sort' link, (which should not make a difference as they are all different values, it messes the rest up.

So, what i would love to see is that, we dont need to sort these with a button but just on page load all rows are correctly merged and grouped, without messing up the previous row and we should end up, if done correctly, with the required format screenshot, so the order of the columns is determined by the values in the row cells, if they are equal.

Thank you guys ever so much, you have helped me out greatly already.

ConnorsFan

If I understand your question correctly, you want to transpose the usual sort command, so that it rearranges the GridView columns according to the order in a row rather than rearranging the rows according to the order in a column.

You can insert a LinkButton in the first column to trigger the sort on a specific row (I used a TemplateField instead of a ButtonField in order to set the text with a binding expression):

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
    OnRowCommand="GridView1_RowCommand" OnPreRender="GridView1_PreRender">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton runat="server" Text='<%# "Row " + ((Container as GridViewRow).RowIndex + 1) %>' CommandName="SortRow" CommandArgument='<%# (Container as GridViewRow).RowIndex %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在后面的代码中,您可以在网格的左侧定义固定列的数量,并sortRowIndexRowCommand事件处理程序中设置一个变量

private const int fixedColumnCount = 1; // Row number column
private int sortRowIndex = -1;

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "SortRow")
    {
        sortRowIndex = Convert.ToInt32(e.CommandArgument);
        // Here set the data source and bind the data to the GridView
        GridView1.DataSource = ...
        GridView1.DataBind();
    }
}

排序本身可以在数据源中执行,正如fnostro在您之前的文章中所建议的那样。由于我不知道数据是什么样子,因此这是在网格中进行处理的一种方法。

这项工作可以在完成PreRender或在DataBoundGridView控件的事件。您在另一篇文章中给出的具有相同内容的单元格分组的代码也应在此事件处理程序中移动,并放在sort操作之后。

protected void GridView1_PreRender(object sender, EventArgs e)
{
    List<int> sortedColIndexes = null;

    if (sortRowIndex >= 0)
    {
        // Gather the cell data on the sorted row...
        List<KeyValuePair<string, int>> cellsToSort = new List<KeyValuePair<string, int>>();
        sortedColIndexes = new List<int>();

        GridViewRow sortedRow = GridView1.Rows[sortRowIndex];
        for (int i = fixedColumnCount; i < sortedRow.Cells.Count; i++)
        {
            TableCell cell = sortedRow.Cells[i];
            cellsToSort.Add(new KeyValuePair<string, int>(cell.Text, i));
        }

        // ... and sort the cell indexes according to the cell content
        sortedColIndexes = cellsToSort.OrderBy(x => x.Key).Select(x => x.Value).ToList();
    }

    RearrangeRowCells(GridView1.HeaderRow, sortedColIndexes);

    foreach (GridViewRow row in GridView1.Rows)
    {
        RearrangeRowCells(row, sortedColIndexes);

        // The code below is taken from the other post
        // It merges the cells with the same content
        for (int i = 0; i < row.Cells.Count - 1; i++)
        {
            TableCell cell = row.Cells[i];

            if (cell.Visible)
            {
                int colSpanValue = 1;

                for (int j = i + 1; j < row.Cells.Count; j++)
                {
                    TableCell otherCell = row.Cells[j];

                    if (otherCell.Text == cell.Text)
                    {
                        colSpanValue++;
                        otherCell.Visible = false;
                    }
                    else
                    {
                        break;
                    }
                }

                if (colSpanValue > 1)
                {
                    cell.ColumnSpan = colSpanValue;
                    cell.BackColor = System.Drawing.Color.Beige;
                    cell.HorizontalAlign = HorizontalAlign.Center;
                }
            }
        }
    }
}

// Utility function which performs the cell permutations on a given row
private void RearrangeRowCells(GridViewRow row, List<int> sortedColIndexes)
{
    if (sortedColIndexes != null)
    {
        List<TableCell> sortedCells = new List<TableCell>();

        foreach (int cellIndex in sortedColIndexes)
        {
            sortedCells.Add(row.Cells[cellIndex]);
        }

        for (int i = fixedColumnCount; i < sortedCells.Count - 1; i++)
        {
            row.Cells.Remove(sortedCells[i]);
            row.Cells.AddAt(i + fixedColumnCount, sortedCells[i]);
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在asp.net中对gridview列进行排序

来自分类Dev

使用asp.net的jQuery Table Sorter对Gridview进行排序

来自分类Dev

在将文件/列表绑定到gridview asp.net C#之前先对其进行排序

来自分类Dev

使用C#在ASP.NET 2中进行GridView排序

来自分类Dev

排序GridView asp.net

来自分类Dev

访问GridView ItemTemplate控件ASP.NET

来自分类Dev

使用Ajax在ASP.NET MVC中异步对GridView进行排序

来自分类Dev

如何在asp.net的GridView中对对象列表进行排序?

来自分类Dev

如何在asp.net的GridView中对对象列表进行排序?

来自分类Dev

如何对ASP.net Web应用程序中的GridView列进行排序

来自分类Dev

asp.net gridview控件与其他控件的绑定

来自分类Dev

到达位于gridview控件内的asp.net控件

来自分类Dev

用户控件中的gridview时,asp.net gridview scrollintoview

来自分类Dev

用户控件中的gridview时,asp.net gridview scrollintoview

来自分类Dev

在asp.net中排序GridView列

来自分类Dev

ASP.net Gridview不排序

来自分类Dev

基于C#中的DropDownList在Asp.net Gridview中启用“禁用”复选框

来自分类Dev

ASP.Net GridView Web控件格式列

来自分类Dev

绑定C#,ASP.net之后在gridview中添加新行

来自分类Dev

Gridview仅显示MySQL数据库中的1行-ASP.net C#

来自分类Dev

asp net c#使用复选框获取gridview中特定行的值

来自分类Dev

如何在asp .net和c#中的gridview中启用选定的行

来自分类Dev

ASP.NET C# 在中继器的 Gridview 中继续行计数

来自分类Dev

将gridview的所有行保存到数据库asp.net c#

来自分类Dev

在Asp.net C#中使用linq按升序和降序对list <object>进行排序

来自分类Dev

asp.net mvc剃刀视图-基于表单控件的值进行重定向

来自分类Dev

ASP.NET GridView 使用 DataTable.Select 排序

来自分类Dev

ASP.NET Gridview 排序不起作用

来自分类Dev

asp.net C# Gridview 嵌套 gridview

Related 相关文章

  1. 1

    在asp.net中对gridview列进行排序

  2. 2

    使用asp.net的jQuery Table Sorter对Gridview进行排序

  3. 3

    在将文件/列表绑定到gridview asp.net C#之前先对其进行排序

  4. 4

    使用C#在ASP.NET 2中进行GridView排序

  5. 5

    排序GridView asp.net

  6. 6

    访问GridView ItemTemplate控件ASP.NET

  7. 7

    使用Ajax在ASP.NET MVC中异步对GridView进行排序

  8. 8

    如何在asp.net的GridView中对对象列表进行排序?

  9. 9

    如何在asp.net的GridView中对对象列表进行排序?

  10. 10

    如何对ASP.net Web应用程序中的GridView列进行排序

  11. 11

    asp.net gridview控件与其他控件的绑定

  12. 12

    到达位于gridview控件内的asp.net控件

  13. 13

    用户控件中的gridview时,asp.net gridview scrollintoview

  14. 14

    用户控件中的gridview时,asp.net gridview scrollintoview

  15. 15

    在asp.net中排序GridView列

  16. 16

    ASP.net Gridview不排序

  17. 17

    基于C#中的DropDownList在Asp.net Gridview中启用“禁用”复选框

  18. 18

    ASP.Net GridView Web控件格式列

  19. 19

    绑定C#,ASP.net之后在gridview中添加新行

  20. 20

    Gridview仅显示MySQL数据库中的1行-ASP.net C#

  21. 21

    asp net c#使用复选框获取gridview中特定行的值

  22. 22

    如何在asp .net和c#中的gridview中启用选定的行

  23. 23

    ASP.NET C# 在中继器的 Gridview 中继续行计数

  24. 24

    将gridview的所有行保存到数据库asp.net c#

  25. 25

    在Asp.net C#中使用linq按升序和降序对list <object>进行排序

  26. 26

    asp.net mvc剃刀视图-基于表单控件的值进行重定向

  27. 27

    ASP.NET GridView 使用 DataTable.Select 排序

  28. 28

    ASP.NET Gridview 排序不起作用

  29. 29

    asp.net C# Gridview 嵌套 gridview

热门标签

归档