在gridview中自定义分组 [英] Custom grouping in a gridview

查看:86
本文介绍了在gridview中自定义分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好亲爱的CPers,



我有一个gridview,我从列表绑定。我想要的是如何在此gridview中应用自定义分组?我不需要代码,但我需要一些建议如何实现这一点。我的意思是自定义分组;用户将选择他们希望分组的行,因此按列分组将不起作用。如果有人向我展示了实现这一目标的道路,我将很高兴。谢谢。

Hello dear CPers,

I have a gridview, that I bind from a list. What I want is how I can apply a custom grouping in this gridview? I dont need code but I need some advise how I can implement this. What I mean by custom grouping; user will select the rows that they desire to group so grouping by columns will not work. I will be glad if someone show me a path accomplishing this. Thanks.

推荐答案

我希望这个样本能帮到你。 GridView:创建论坛和摘要 [ ^ ]
I hope this sample will help you well. GridView: Creating groups and summaries[^]


用于在gridview中对行进行分组的Genric方法



Genric method for Grouping the rows in gridview

void GroupGridView(GridViewRowCollection gvrc, int startIndex, int total)
    {
        if (total == 0) return;
        int i, count = 1;
        ArrayList lst = new ArrayList();
        lst.Add(gvrc[0]);
        var ctrl = gvrc[0].Cells[startIndex];
        for (i = 1; i < gvrc.Count; i++)
        {
            TableCell nextCell = gvrc[i].Cells[startIndex];
            if (ctrl.Text == nextCell.Text)
            {
                count++;
                nextCell.Visible = false;
                lst.Add(gvrc[i]);
            }
            else
            {
                if (count > 1)
                {
                    ctrl.RowSpan = count;
                    GroupGridView(new GridViewRowCollection(lst), startIndex + 1, total - 1);
                }
                count = 1;
                lst.Clear();
                ctrl = gvrc[i].Cells[startIndex];
                lst.Add(gvrc[i]);
            }
        }
        if (count > 1)
        {
            ctrl.RowSpan = count;
            GroupGridView(new GridViewRowCollection(lst), startIndex + 1, total - 1);
        }
        count = 1;
        lst.Clear();
    }







You have to pass 3 parameters:
gvrc: GridView Rows
startIndex: index of first column to be grouped(where to start grouping).
total: total number of columns to be grouped.





如何使用:



在使用此方法之前,请确保数据的排序顺序与它们的分组顺序相同。绑定gridview并传递gridview行,开始索引和要在GroupGridView方法中分组的总列数并享受它。



1

2

3



GridView1.DataSource = GetDataTable();

GridView1.DataBind();

GroupGridView(GridView1.Rows,0,3);



上面的代码将分组gridview的前3列。



希望,这会节省您的时间。



How to Use:

Before using this method, Make sure data is sorted in same order in which they are to be grouped. Bind the gridview and Pass the gridview rows, start index and total number of columns to be grouped in GroupGridView method and enjoy it.
?
1
2
3

GridView1.DataSource = GetDataTable();
GridView1.DataBind();
GroupGridView(GridView1.Rows, 0, 3);

The above code will group first 3 columns of gridview.

Hope, It’ll save your time.


这篇关于在gridview中自定义分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆