给DataGrid添加滚动条并固定表头
来源:互联网 发布:自学音乐软件 编辑:程序博客网 时间:2024/05/14 14:05
1.html
<HTML>
<HEAD>
<title>ScrollGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style>
.fixedHeaderTr {}{ POSITION: relative; ; TOP: expression(this.offsetParent.scrollTop) }
.mainDiv {}{ SCROLLBAR-FACE-COLOR: #9999ff; OVERFLOW: auto; ; WIDTH: expression(document.body.clientWidth-20); ; HEIGHT: expression((document.body.clientHeight-this.offsetTop-20>this.children[0].offsetHeight)?(this.children[0].offsetHeight+20) : (document.body.clientHeight-this.offsetTop-20)) }
</style>
</HEAD>
<BODY ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<div class="mainDiv">
<asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
</div>
</form>
</BODY>
</HTML>
2.cs 代码<HEAD>
<title>ScrollGrid</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style>
.fixedHeaderTr {}{ POSITION: relative; ; TOP: expression(this.offsetParent.scrollTop) }
.mainDiv {}{ SCROLLBAR-FACE-COLOR: #9999ff; OVERFLOW: auto; ; WIDTH: expression(document.body.clientWidth-20); ; HEIGHT: expression((document.body.clientHeight-this.offsetTop-20>this.children[0].offsetHeight)?(this.children[0].offsetHeight+20) : (document.body.clientHeight-this.offsetTop-20)) }
</style>
</HEAD>
<BODY ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<div class="mainDiv">
<asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
</div>
</form>
</BODY>
</HTML>
public class ScrollGrid : System.Web.UI.Page
{
//记得要引用
//using System.Data.SqlClient;
//using System.IO;
//using System.Text.RegularExpressions;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}
Render#region Render
protected override void Render(HtmlTextWriter writer)
{
//原理就是把表头<tr></tr>标签替换为<tr class="fixedHeaderTr"></tr>
TextWriter tempWriter = new StringWriter();
base.Render(new HtmlTextWriter(tempWriter));
string oldtr = "<tr style=/"color:#FFFFCC;background-color:#990000;font-weight:bold;/">";
string newtr = "<tr class=/"fixedHeaderTr/" style=/"color:#FFFFCC;background-color:#990000;font-weight:bold;/">";
writer.Write(Regex.Replace(tempWriter.ToString(),oldtr,newtr,RegexOptions.IgnoreCase));
}
#endregion
DataBind#region DataBind
private void DataBind()
{
string sql="select * from testgrid";
DataSet ds=GetDataSet(sql);
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
}
#endregion
GetDataSet#region GetDataSet
private DataSet GetDataSet(string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
3.源代码下载/Files/singlepine/WebApplication1.rar{
//记得要引用
//using System.Data.SqlClient;
//using System.IO;
//using System.Text.RegularExpressions;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
DataBind();
}
Render#region Render
protected override void Render(HtmlTextWriter writer)
{
//原理就是把表头<tr></tr>标签替换为<tr class="fixedHeaderTr"></tr>
TextWriter tempWriter = new StringWriter();
base.Render(new HtmlTextWriter(tempWriter));
string oldtr = "<tr style=/"color:#FFFFCC;background-color:#990000;font-weight:bold;/">";
string newtr = "<tr class=/"fixedHeaderTr/" style=/"color:#FFFFCC;background-color:#990000;font-weight:bold;/">";
writer.Write(Regex.Replace(tempWriter.ToString(),oldtr,newtr,RegexOptions.IgnoreCase));
}
#endregion
DataBind#region DataBind
private void DataBind()
{
string sql="select * from testgrid";
DataSet ds=GetDataSet(sql);
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
}
#endregion
GetDataSet#region GetDataSet
private DataSet GetDataSet(string sql)
{
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda =new SqlDataAdapter(sql,constring);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
代码里面的数据库及web.config要自己修改。
- 给DataGrid添加滚动条并固定表头
- 给DataGrid添加滚动条并固定表头
- 为表格添加竖直滚动条并固定表头
- 固定表头,加滚动条的datagrid
- 给DataGrid添加下拉滚动条
- 给Datagrid添加横向的滚动条
- table中资料过多,表头固定,清单添加滚动条
- GridView(table) 滚动条 固定表头、列
- 一个简单的给DataGrid添加滚动条
- 给DataGrid创建滚动条
- datagrid中添加滚动条
- datagrid中添加滚动条
- Javascript实现GridView,表头固定,表体有滚动条可滚动
- Flex 中给Datagrid表头添加按钮
- 创建固定表头、表格体滚动的DataGrid
- 创建固定表头、表格体滚动的DataGrid
- 创建固定表头、表格体滚动的DataGrid(c#版)
- 创建固定表头、表格体滚动的DataGrid
- P2P的Jxta解决方案
- J2EE or J2SE? JNDI works with both(from javaworld)
- 实现类似split功能(charindex)
- 加载已有项目出现 http/1.1 403 access forbidden 错误
- 纵向查询显示小示例
- 给DataGrid添加滚动条并固定表头
- 访问控制列表加注释的方式。
- GUI设计感悟
- 再报惊喜!SWT Designer 6.0 (Window Builder Pro) 注册机
- 导出导入Excel(DataSet,DataGrid)
- 用ZedGraph作图表(二)
- 正则表达式学习
- 教你做一个优秀的项目经理
- MD5算法之C#程序 MD5算法描述