65.9K
CodeProject 正在变化。 阅读更多。
Home

带有冻结表头和排序图像的可滚动 Gridview。

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.35/5 (16投票s)

2008年5月14日

CPOL
viewsIcon

170351

downloadIcon

5147

这是一种轻松实现冻结 GridView 表头并添加排序图像的方法。它适用于 IE、Firefox 和其他浏览器,可以在 UpdatePanel 内部和外部工作。

AntonioSuarez_gridview_ff.JPGAntonioSuarez_gridview_ie.JPG

引言

我一直在寻找一种实现**可滚动** **Gridview** 并具有**冻结** **表头**的方法,并且找到了许多不同的方法,但其中大多数都使用 CSS 或 JavaScript。我想要找到的是一种在**代码文件**中实现的方法(.CS 或 .VB)。

在这里,我将尝试解释如何用代码来实现它(.CS)。

Using the Code

你只需要添加一个 **Panel** 控制,然后在 Panel 控制内部添加一个 **Table** 和 **Gridview** 控制。

在 .CS 文件中,你需要添加以下代码

AddSortDirectionImage 函数

 
    /// summary
    ///   Email: Antonio.Suarez@GrupoKino.com
    ///Web Page: http://www.grupoKino.com    
    /// [Español]Funcion que agrega una imagen a la columna ordenada de un gridView 
    /// [English]Function that adds a image to the sorted column.
    /// summary
    /// param name="gridviewID"
    /// [Español]Gridview al cual se le agregara la imagen 
    /// [English]GridView to add sort image
    /// param
    /// param name="itemRow"
    /// [Español]Renglon del gridview al cual se le agrega la imagen 
    /// [English]Row of gridview to add sort image
    /// param

    public void AddSortDirectionImage(GridView gridviewID, GridViewRow itemRow)
    {
        if (gridviewID.AllowSorting == false)
            return;

        Image sortImage = new Image();
        Label space = new Label();

        sortImage.ImageUrl = (
            gridviewID.SortDirection ==
            SortDirection.Ascending ? @"~/sort_asc.gif" : @"~/sort_desc.gif");
        sortImage.Visible = true;
        space.Text = " ";


        for (int i = 0; i < gridviewID.Columns.Count; i++)
        {
            string colExpr = gridviewID.Columns[i].SortExpression;
            if (colExpr != "" && colExpr == gridviewID.SortExpression)
            {
                itemRow.Cells[i].Controls.Add(space);
                itemRow.Cells[i].Controls.Add(sortImage);
            }
        }
    }

和 FreezeGridviewHeader 函数

 

    /// summary
    ///  Author: Antonio Suarez
    ///   Email: Antonio.Suarez@GrupoKino.com
    ///Web Page: http://www.grupoKino.com    
    /// [Español]Funcion que copia el header de un gridview a un control tipo Table 
    /// [English]Function that copy a gridview header to a table control.
    /// summary
    /// param name="_gv1"
    /// [Español]Gridview del cual se copiara el encabezado.
    /// [English]GridView from where the header row will be copied
    /// param
    /// param name="_tb1"
    /// [Español]Renglon del gridview al cual se le agrega la imagen 
    /// [English]Row of gridview to add sort image
    /// 
    /// param name="_pc1"
    /// [Español]Panel que contendra tanto al gridview como al control table
    /// [English]Panel container of the gridview and table control.   
    /// param    
    protected void FreezeGridviewHeader(GridView _gv1, Table _tb1,Panel _pc1)
    {
        Page.EnableViewState = false;

        //[Español]Copiando las propiedades del renglon de encabezado
        //[English]Copying a header row data and properties    
        _tb1.Rows.Add(_gv1.HeaderRow);
        _tb1.Rows[0].ControlStyle.CopyFrom(_gv1.HeaderStyle);
        _tb1.CellPadding = _gv1.CellPadding;
        _tb1.CellSpacing = _gv1.CellSpacing;
        _tb1.BorderWidth = _gv1.BorderWidth;

        //if (!_gv1.Width.IsEmpty)
        //_gv1.Width = Unit.Pixel(Convert.ToInt32(_gv1.Width.Value) + Convert.ToInt32(
        //    _tb1.Width.Value) + 13);

        //[Español]Copiando las propiedades de cada celda del nuevo encabezado.
        //[English]Copying  each cells properties to the new header cells properties
        int Count = 0;
        _pc1.Width = Unit.Pixel(100);
        for (Count = 0; Count < _gv1.HeaderRow.Cells.Count - 1; Count++)
        {
            _tb1.Rows[0].Cells[Count].Width = _gv1.Columns[Count].ItemStyle.Width;
            _tb1.Rows[0].Cells[Count].BorderWidth = 
                _gv1.Columns[Count].HeaderStyle.BorderWidth;
            _tb1.Rows[0].Cells[Count].BorderStyle = 
                _gv1.Columns[Count].HeaderStyle.BorderStyle;
            _pc1.Width = Unit.Pixel(Convert.ToInt32(
                _tb1.Rows[0].Cells[Count].Width.Value) +
                Convert.ToInt32(_pc1.Width.Value) + 14);
        }
        //Panel1.Width = Unit.Pixel(Convert.ToInt32(
        //    _tb1.Rows[0].Cells[Count-1].Width.Value) + 12);
    }

关注点

这里的重点是,它应该在**大多数**图形**浏览器**中**工作**,并且我们**不需要为每个浏览器的兼容性添加修复程序**。

历史

我们需要添加编辑功能。希望这些对你有所帮助,如果一切正常。

另外,如果你有网页,请添加我的网页链接 **http://www.grupoKino.com**


Antonio Suarez。
© . All rights reserved.