ASP.NET GridView的第二个标题行跨越主标题行 [英] ASP.NET GridView second header row to span main header row

查看:169
本文介绍了ASP.NET GridView的第二个标题行跨越主标题行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET的GridView有看起来像这样的栏目:

I have an ASP.NET GridView which has columns that look like this:

| Foo | Bar | Total1 | Total2 | Total3 |

是否有可能创建两行头,看起来像这样?

Is it possible to create a header on two rows that looks like this?

|           |  Totals   |    
| Foo | Bar | 1 | 2 | 3 |

每行中的数据将保持不变,因为这仅仅是pretty了头,并在网格占用降低了水平空间。

The data in each row will remain unchanged as this is just to pretty up the header and decrease the horizontal space that the grid takes up.

整个GridView的是,重要情况排序。我不打算为添加的总计栏跨越有任何排序功能。

The entire GridView is sortable in case that matters. I don't intend for the added "Totals" spanning column to have any sort functionality.

编辑:

根据下面给出的文章之一,我创建了从GridView的继承和添加第二个标题行。一类

Based on one of the articles given below, I created a class which inherits from GridView and adds the second header row in.

namespace CustomControls
{
    public class TwoHeadedGridView : GridView
    {
        protected Table InnerTable
        {
            get
            {
                if (this.HasControls())
                {
                    return (Table)this.Controls[0];
                }

                return null;
            }
        }

        protected override void OnDataBound(EventArgs e)
        {
            base.OnDataBound(e);
            this.CreateSecondHeader();
        }

        private void CreateSecondHeader()
        {
            GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);

            TableCell left = new TableHeaderCell();
            left.ColumnSpan = 3;
            row.Cells.Add(left);

            TableCell totals = new TableHeaderCell();
            totals.ColumnSpan = this.Columns.Count - 3;
            totals.Text = "Totals";
            row.Cells.Add(totals);

            this.InnerTable.Rows.AddAt(0, row);
        }
    }
}

如果你是新的ASP.NET喜欢我,我也应该指出的是,你需要:

In case you are new to ASP.NET like I am, I should also point out that you need to:

1)加入这样一行到您的Web表单填写您的类:

1) Register your class by adding a line like this to your web form:

<%@ Register TagPrefix="foo" NameSpace="CustomControls" Assembly="__code"%>

2)更改ASP:GridView控件在previous标记为foo:TwoHeadedGridView。不要忘了关闭标签。

2) Change asp:GridView in your previous markup to foo:TwoHeadedGridView. Don't forget the closing tag.

另一个编辑:

您也可以做到这一点无需创建自定义类。

You can also do this without creating a custom class.

只需添加的事件处理程序网格像这样的DataBound事件:

Simply add an event handler for the DataBound event of your grid like this:

protected void gvOrganisms_DataBound(object sender, EventArgs e)
{
    GridView grid = sender as GridView;

    if (grid != null)
    {
        GridViewRow row = new GridViewRow(0, -1,
            DataControlRowType.Header, DataControlRowState.Normal);

        TableCell left = new TableHeaderCell();
        left.ColumnSpan = 3;
        row.Cells.Add(left);

        TableCell totals = new TableHeaderCell();
        totals.ColumnSpan = grid.Columns.Count - 3;
        totals.Text = "Totals";
        row.Cells.Add(totals);

        Table t = grid.Controls[0] as Table;
        if (t != null)
        {
            t.Rows.AddAt(0, row);
        }
    }
}

自定义控制的好处是,你可以看到你的Web表单的设计视图额外的标题行。事件处理程序方法有点简单,但。

The advantage of the custom control is that you can see the extra header row on the design view of your web form. The event handler method is a bit simpler, though.

推荐答案

本文应该指向你在正确的方向。您可以创建程式设计的行和它在位置0添加到集合中。

This article should point you in the right direction. You can programtically create the row and add it to the collection at position 0.

这篇关于ASP.NET GridView的第二个标题行跨越主标题行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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