那就是冻结列啦 给你个例子吧 ServerAndClientSide.aspx: ----------------------------------------------- <%@ Page Language="vb" AutoEventWireup="false" Inherits="Testing.ServerAndClientSide" CodeFile="ServerAndClientSide.aspx.vb" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>Client Side Column Locking</title> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> <LINK href="Styles.css" type="text/css" rel="stylesheet"> <script type="text/javascript"> function lockCol(tblID) { var table = document.getElementById(tblID); var button = document.getElementById('toggle'); var cTR = table.getElementsByTagName('tr'); //collection of rows if (table.rows[0].cells[0].className == '') { for (i = 0; i < cTR.length; i++) { var tr = cTR.item(i); tr.cells[0].className = 'locked' } button.innerText = "Unlock First Column"; } else { for (i = 0; i < cTR.length; i++) { var tr = cTR.item(i); tr.cells[0].className = '' } button.innerText = "Lock First Column"; } } </script> </HEAD> <body> <form id="Form1" method="post" runat="server"> <P> </P> <button id="toggle" onclick="lockCol('DataGrid1')" type="button">Unlock First Column</button> <div id="div-datagrid"> <asp:DataGrid id="DataGrid1" runat="server" CssClass="Grid" UseAccessibleHeader="True" AutoGenerateColumns=False> <AlternatingItemStyle CssClass="GridAltRow"></AlternatingItemStyle> <ItemStyle CssClass="GridRow"></ItemStyle> <Columns> <asp:BoundColumn DataField="Name" HeaderText="Name"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="Address" HeaderText="Address"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="City" HeaderText="City"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="State" HeaderText="State"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="Zip" HeaderText="Zip"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="Random Babble" HeaderText="Random Babble"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> </Columns> </asp:DataGrid></div> <p></p> <p></p> </form> </body> </HTML> ServerAndClientSide.aspx.vb: ------------------------------------------------------------- Namespace TestingPartial Class ServerAndClientSide Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. ' <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() ' End Sub 'Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init ' 'CODEGEN: This method call is required by the Web Form Designer ' 'Do not modify it using the code editor. ' InitializeComponent() 'End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim d As New MyData DataGrid1.DataSource = d.CreateDataSource() DataGrid1.DataBind() End If End Sub Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles DataGrid1.ItemDataBound e.Item.Cells(0).CssClass = "locked" End SubEnd ClassEnd Namespace Styles.css --------------------------------------------------------------- /* Div container to wrap the datagrid */ div#div-datagrid { width: 420px; height: 200px; overflow: auto; scrollbar-base-color:#ffeaff; }/* Locks the left column */ td.locked, th.locked { font-size: 14px; font-weight: bold; text-align: center; background-color: navy; color: white; border-right: 1px solid silver; position:relative; cursor: default; left: expression(document.getElementById("div-datagrid").scrollLeft-2); /*IE5+ only*/ } /* Locks table header */ th { font-size: 14px; font-weight: bold; text-align: center; background-color: navy; color: white; border-right: 1px solid silver; position:relative; cursor: default; top: expression(document.getElementById("div-datagrid").scrollTop-2); /*IE5+ only*/ z-index: 10; }/* Keeps the header as the top most item. Important for top left item*/ th.locked {z-index: 99;} /* DataGrid Item and AlternatingItem Style*/ .GridRow {font-size: 10pt; color: black; font-family: Arial; background-color:#ffffff; height:35px;} .GridAltRow {font-size: 10pt; color: black; font-family: Arial; background-color:#eeeeee; height:35px;}App_Code\MyData.vb ---------------------------------------------------------------------- Namespace TestingPublic Class MyData Function CreateDataSource() As ICollection Dim dt As DataTable 'create a DataTable dt = New DataTable dt.Columns.Add(New DataColumn("Name", GetType(String))) dt.Columns.Add(New DataColumn("Address", GetType(String))) dt.Columns.Add(New DataColumn("City", GetType(String))) dt.Columns.Add(New DataColumn("State", GetType(String))) dt.Columns.Add(New DataColumn("Zip", GetType(String))) dt.Columns.Add(New DataColumn("Random Babble", GetType(String))) Dim i As Integer = 0 For i = 0 To 50 Dim dr1 As DataRow dr1 = dt.NewRow dr1(0) = "Dave Lewis" dr1(1) = "533 My Address" dr1(2) = "Columbus" dr1(3) = "Ohio" dr1(4) = "43230" dr1(5) = "adfkasdfasdfsdfa sadfa dfa aksdjfkasajskdfjaf kasdjfakfjdfkjaklsdf adksfjkalsfd" dt.Rows.Add(dr1) Next 'return a DataView to the DataTable Return New DataView(dt) End Function End ClassEnd Namespace
ServerAndClientSide.aspx:
-----------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Inherits="Testing.ServerAndClientSide" CodeFile="ServerAndClientSide.aspx.vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Client Side Column Locking</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="Styles.css" type="text/css" rel="stylesheet">
<script type="text/javascript">
function lockCol(tblID) { var table = document.getElementById(tblID);
var button = document.getElementById('toggle');
var cTR = table.getElementsByTagName('tr'); //collection of rows if (table.rows[0].cells[0].className == '') {
for (i = 0; i < cTR.length; i++)
{
var tr = cTR.item(i);
tr.cells[0].className = 'locked'
}
button.innerText = "Unlock First Column";
}
else {
for (i = 0; i < cTR.length; i++)
{
var tr = cTR.item(i);
tr.cells[0].className = ''
}
button.innerText = "Lock First Column";
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P> </P>
<button id="toggle" onclick="lockCol('DataGrid1')" type="button">Unlock First Column</button>
<div id="div-datagrid">
<asp:DataGrid id="DataGrid1" runat="server" CssClass="Grid" UseAccessibleHeader="True" AutoGenerateColumns=False>
<AlternatingItemStyle CssClass="GridAltRow"></AlternatingItemStyle>
<ItemStyle CssClass="GridRow"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Name">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Address" HeaderText="Address">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="City" HeaderText="City">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="State" HeaderText="State">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Zip" HeaderText="Zip">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Random Babble" HeaderText="Random Babble">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid></div>
<p></p>
<p></p>
</form>
</body>
</HTML>
ServerAndClientSide.aspx.vb:
-------------------------------------------------------------
Namespace TestingPartial Class ServerAndClientSide
Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer.
' <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() ' End Sub
'Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
' 'CODEGEN: This method call is required by the Web Form Designer
' 'Do not modify it using the code editor.
' InitializeComponent()
'End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then
Dim d As New MyData
DataGrid1.DataSource = d.CreateDataSource()
DataGrid1.DataBind()
End If
End Sub Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
e.Item.Cells(0).CssClass = "locked"
End SubEnd ClassEnd Namespace
Styles.css
---------------------------------------------------------------
/* Div container to wrap the datagrid */
div#div-datagrid {
width: 420px;
height: 200px;
overflow: auto;
scrollbar-base-color:#ffeaff;
}/* Locks the left column */
td.locked, th.locked {
font-size: 14px;
font-weight: bold;
text-align: center;
background-color: navy;
color: white;
border-right: 1px solid silver;
position:relative;
cursor: default;
left: expression(document.getElementById("div-datagrid").scrollLeft-2); /*IE5+ only*/
} /* Locks table header */
th {
font-size: 14px;
font-weight: bold;
text-align: center;
background-color: navy;
color: white;
border-right: 1px solid silver;
position:relative;
cursor: default;
top: expression(document.getElementById("div-datagrid").scrollTop-2); /*IE5+ only*/
z-index: 10;
}/* Keeps the header as the top most item. Important for top left item*/
th.locked {z-index: 99;}
/* DataGrid Item and AlternatingItem Style*/
.GridRow {font-size: 10pt; color: black; font-family: Arial; background-color:#ffffff; height:35px;}
.GridAltRow {font-size: 10pt; color: black; font-family: Arial; background-color:#eeeeee; height:35px;}App_Code\MyData.vb
----------------------------------------------------------------------
Namespace TestingPublic Class MyData
Function CreateDataSource() As ICollection Dim dt As DataTable 'create a DataTable
dt = New DataTable
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Address", GetType(String)))
dt.Columns.Add(New DataColumn("City", GetType(String)))
dt.Columns.Add(New DataColumn("State", GetType(String)))
dt.Columns.Add(New DataColumn("Zip", GetType(String)))
dt.Columns.Add(New DataColumn("Random Babble", GetType(String))) Dim i As Integer = 0
For i = 0 To 50
Dim dr1 As DataRow
dr1 = dt.NewRow
dr1(0) = "Dave Lewis"
dr1(1) = "533 My Address"
dr1(2) = "Columbus"
dr1(3) = "Ohio"
dr1(4) = "43230"
dr1(5) = "adfkasdfasdfsdfa sadfa dfa aksdjfkasajskdfjaf kasdjfakfjdfkjaklsdf adksfjkalsfd"
dt.Rows.Add(dr1)
Next 'return a DataView to the DataTable
Return New DataView(dt) End Function
End ClassEnd Namespace
你自己好好看看吧,是通过CSS来控制表头和列的冻结效果
4楼的例子是固定标题列.应该不是楼主想要的结果!你可以去孟子的网站看看.上面有你要的实例!
-----------------------------------------------------
除了固定header以外 还能固定多列 不知道你仔细看了没有!