博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

gridview固定表头

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
          //该行的每一个单元格
                for (int j = 0; j < e.Row.Cells.Count; j++)
                {
                    //单元格不属于固定列
                    if (string.IsNullOrEmpty(e.Row.Cells[j].Attributes["style"]) || e.Row.Cells[j].Attributes["style"].IndexOf("position:relative;") == -1)
                    {
                        e.Row.Cells[j].Attributes.Add("style", "position:relative;top:expression(this.offsetParent.scrollTop-0);");

                    }
                    else
                    {
                        e.Row.Cells[j].Attributes.Add("style", e.Row.Cells[j].Attributes["style"] + "top:expression(this.offsetParent.scrollTop-0);z-index:510");
                    }
                }
}

当表格数据为空时显示表头方法

    //当Gridview数据为空时显示的信息
        private static string EmptyText = "&nbsp;";


    /// <summary>
        /// 绑定数据到GridView,当表格数据为空时显示表头
        /// </summary>
        /// <param name="gridview"></param>
        /// <param name="table"></param>
        public static void GridViewDataBind(GridView gridview, DataTable table)
        {
            //记录为空重新构造Gridview
            if (table.Rows.Count == 0)
            {
                (gridview as ECIGridView).SetHasData("N");
                table = table.Clone();
                table.Rows.Add(table.NewRow());
                gridview.DataSource = table;
                gridview.DataBind();

                int columnCount = gridview.Columns.Count;
                if (gridview.AutoGenerateColumns) columnCount = table.Columns.Count + 1;
                gridview.Rows[0].Cells.Clear();
                gridview.Rows[0].Cells.Add(new TableCell());
                gridview.Rows[0].Cells[0].ColumnSpan = columnCount;
                gridview.Rows[0].Cells[0].Text = EmptyText;
                gridview.Rows[0].Cells[0].Style.Add("text-align", "center");
            }
            else
            {
                (gridview as ECIGridView).SetHasData("Y");
                //数据不为空直接绑定
                gridview.DataSource = table;
                gridview.DataBind();
            }

            //重新绑定取消选择
            gridview.SelectedIndex = -1;
        }

GridView单元格合并

protected void gvData_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        ListItemType lit = e.Item.ItemType;
        if (ListItemType.Header == lit)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
        }
    }
    private void NewRenderMethod(HtmlTextWriter writer, System.Web.UI.Control ctl)
    {
        for (int j = 0; j < 8; j++)
        {
            TableCell cell = (TableCell)ctl.Controls[j];
            cell.Attributes.Add("rowspan", "2");
            cell.RenderControl(writer);
        }
        

        int HUSDcount = int.Parse(ALUSDAR[0].ToString());

        if (HUSDcount > 0)//
        {
            writer.Write("<TD colspan=" + HUSDcount + " align=\"center\" >进场费</TD>\n");
        }
        //writer.Write("<TD rowspan=2>jjjj</TD>\n");
        writer.Write("</TR>\n");
        writer.RenderBeginTag("TR");

        for (int i = 8; i < ctl.Controls.Count; i++)
        {
            
            //TableCell cell = (TableCell)ctl.Controls[i];
            //cell.Visible = false;

            ctl.Controls[i].RenderControl(writer);
        }
        
        DatagridCount++;
    }