4、重写OnRowDataBound以设置每个单元格的样式,从而实现固定表头、指定行或指定列的功能。
InBlock.gif 
/// <summary> 

InBlock.gif                
/// OnRowDataBound 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="e"></param> 

InBlock.gif                
protected 
override 
void OnRowDataBound(GridViewRowEventArgs e) 

InBlock.gif                { 

InBlock.gif                        
if (e.Row.RowType == DataControlRowType.Pager) 

InBlock.gif                        { 

InBlock.gif                                
if (FixRowCol.IsFixPager) 

InBlock.gif                                { 

InBlock.gif                                        
if (
this.PagerSettings.Position == PagerPosition.Top || (
this.PagerSettings.Position == PagerPosition.TopAndBottom && _isTopPager)) 

InBlock.gif                                        { 

InBlock.gif                                                
// TopPager固定行和列 

InBlock.gif                                                e.Row.Cells[0].Attributes.Add(
"style"
"z-index:999; position: relative; top: expression(this.offsetParent.scrollTop); left: expression(this.offsetParent.scrollLeft);"); 

InBlock.gif                                                
// 现在是TopPager,之后就是BottomPager了,所以设置_isTopPager为false 

InBlock.gif                                                _isTopPager = 
false

InBlock.gif                                        } 

InBlock.gif                                        
else 
if (
this.PagerSettings.Position == PagerPosition.TopAndBottom && !_isTopPager) 

InBlock.gif                                        { 

InBlock.gif                                                
// BottomPager只固定列 

InBlock.gif                                                e.Row.Cells[0].Attributes.Add(
"style"
"z-index:999; position: relative; left: expression(this.offsetParent.scrollLeft);"); 

InBlock.gif                                                
// 现在是BottomPager,之后就是TopPager了,所以设置_isTopPager为true 

InBlock.gif                                                _isTopPager = 
true

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) 

InBlock.gif                        { 

InBlock.gif                                
// 给每一个指定固定的列的单元格加上css属性 

InBlock.gif                                
if (!String.IsNullOrEmpty(FixRowCol.FixColumnIndices)) 

InBlock.gif                                { 

InBlock.gif                                        
// 列索引 

InBlock.gif                                        
foreach (
string s 
in FixRowCol.FixColumnIndices.Split(',')) 

InBlock.gif                                        { 

InBlock.gif                                                
int i; 

InBlock.gif                                                
if (!Int32.TryParse(s, 
out i)) 

InBlock.gif                                                        
throw 
new ArgumentException(
"FixColumnIndices"
"含有非整形的字符"); 

InBlock.gif                                                
if (i > e.Row.Cells.Count) 

InBlock.gif                                                        
throw 
new ArgumentOutOfRangeException(
"FixColumnIndices"
"溢出"); 

InBlock.gif 

InBlock.gif                                                e.Row.Cells[i].Attributes.Add(
"style"
"position: relative; left: expression(this.offsetParent.scrollLeft);"); 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif 

InBlock.gif                                
bool isFixRow = 
false
// 当前行是否固定 

InBlock.gif                                
if (FixRowCol.IsFixHeader && e.Row.RowType == DataControlRowType.Header) 

InBlock.gif                                { 

InBlock.gif                                        isFixRow = 
true

InBlock.gif                                } 

InBlock.gif 

InBlock.gif                                
if (!String.IsNullOrEmpty(FixRowCol.FixRowIndices) && e.Row.RowType == DataControlRowType.DataRow) 

InBlock.gif                                { 

InBlock.gif                                        
// 行索引 

InBlock.gif                                        
foreach (
string s 
in FixRowCol.FixRowIndices.Split(',')) 

InBlock.gif                                        { 

InBlock.gif                                                
int i; 

InBlock.gif                                                
if (!Int32.TryParse(s, 
out i)) 

InBlock.gif                                                        
throw 
new ArgumentException(
"FixRowIndices"
"含有非整形的字符"); 

InBlock.gif                                                
if (i > e.Row.Cells.Count) 

InBlock.gif                                                        
throw 
new ArgumentOutOfRangeException(
"FixRowIndices"
"溢出"); 

InBlock.gif 

InBlock.gif                                                
if (i == e.Row.RowIndex) 

InBlock.gif                                                { 

InBlock.gif                                                        isFixRow = 
true

InBlock.gif                                                        
break

InBlock.gif                                                } 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif 

InBlock.gif                                
// 固定该行 

InBlock.gif                                
if (isFixRow) 

InBlock.gif                                { 

InBlock.gif                                        
// 该行的每一个单元格 

InBlock.gif                                        
for (
int j = 0; j < e.Row.Cells.Count; j++) 

InBlock.gif                                        { 

InBlock.gif                                                
// 该单元格不属于固定列 

InBlock.gif                                                
if (String.IsNullOrEmpty(e.Row.Cells[j].Attributes[
"style"]) || e.Row.Cells[j].Attributes[
"style"].IndexOf(
"position: relative;") == -1) 

InBlock.gif                                                { 

InBlock.gif                                                        e.Row.Cells[j].Attributes.Add(
"style"
" position: relative; top: expression(this.offsetParent.scrollTop);"); 

InBlock.gif                                                } 

InBlock.gif                                                
// 该单元格属于固定列 

InBlock.gif                                                
else 

InBlock.gif                                                { 

InBlock.gif                                                        e.Row.Cells[j].Attributes.Add(
"style", e.Row.Cells[j].Attributes[
"style"] + 
"top: expression(this.offsetParent.scrollTop); z-index: 666;"); 

InBlock.gif                                                } 

InBlock.gif                                        } 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        
base.OnRowDataBound(e); 

InBlock.gif                }

5、增加两个私有变量
InBlock.gif    
/// <summary> 

InBlock.gif                
/// 如果固定行、列的话 滚动条的x位置 

InBlock.gif                
/// </summary> 

InBlock.gif                
private 
int _yy_SmartGridView_x; 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 如果固定行、列的话 滚动条的y位置 

InBlock.gif                
/// </summary> 

InBlock.gif                
private 
int _yy_SmartGridView_y;
 
6、重写GridView的OnPreRender方法,用于注册两个HiddenField,以及注册设置GridView的滚动条的位置的javascript代码
/// <summary> 

InBlock.gif                
/// OnPreRender 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="e"></param> 

InBlock.gif                
protected 
override 
void OnPreRender(EventArgs e) 

InBlock.gif                { 

InBlock.gif                        
if (FixRowCol.EnableScrollState) 

InBlock.gif                        { 

InBlock.gif                                
// 滚动条x位置 

InBlock.gif                                Page.ClientScript.RegisterHiddenField(
"yy_SmartGridView_x", _yy_SmartGridView_x.ToString()); 

InBlock.gif                                
// 滚动条y位置 

InBlock.gif                                Page.ClientScript.RegisterHiddenField(
"yy_SmartGridView_y", _yy_SmartGridView_y.ToString()); 

InBlock.gif 

InBlock.gif                                
// 设置GridView的滚动条的位置 

InBlock.gif                                Page.ClientScript.RegisterStartupScript( 

InBlock.gif                                        
this.GetType(), 

InBlock.gif                                        
"jsSetScroll"
"<script type=\"text/javascript\">document.getElementById('yy_ScrollDiv').scrollLeft=" + _yy_SmartGridView_x + 
";document.getElementById('yy_ScrollDiv').scrollTop=" + _yy_SmartGridView_y + 
";</script>" 

InBlock.gif                                        ); 

InBlock.gif 

InBlock.gif                                
// 将控件注册为要求在页回发至服务器时进行回发处理的控件 

InBlock.gif                                
if (Page != 
null) Page.RegisterRequiresPostBack(
this); 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        
base.OnPreRender(e); 

InBlock.gif                }
 
7、重写GridView的Render方法,将GridView用一个div包裹起来。
/// <summary> 

InBlock.gif                
/// Render 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="writer"></param> 

InBlock.gif                
protected 
override 
void Render(HtmlTextWriter writer) 

InBlock.gif                { 

InBlock.gif                        
// 给GridView一个容器 <div> 

InBlock.gif                        
if (!FixRowCol.TableWidth.IsEmpty || !FixRowCol.TableHeight.IsEmpty) 

InBlock.gif                        { 

InBlock.gif                                
if (FixRowCol.TableWidth.IsEmpty) FixRowCol.TableWidth = 
new Unit(100, UnitType.Percentage); 

InBlock.gif                                
if (FixRowCol.TableHeight.IsEmpty) FixRowCol.TableHeight = 
new Unit(100, UnitType.Percentage); 

InBlock.gif 

InBlock.gif                                writer.Write(
"<div id='yy_ScrollDiv' style=\"overflow: auto; width: " 

InBlock.gif                                        + FixRowCol.TableWidth.ToString() + 
"; height: " 

InBlock.gif                                        + FixRowCol.TableHeight.ToString() + 
"; position: relative;\" "); 

InBlock.gif 

InBlock.gif                                
// 如果保持滚动条的状态的话,用隐藏字段记录滚动条的位置 

InBlock.gif                                
if (FixRowCol.EnableScrollState) 

InBlock.gif                                { 

InBlock.gif                                        writer.Write(
"οnscrοll=\"document.getElementById('yy_SmartGridView_x').value = this.scrollLeft; document.getElementById('yy_SmartGridView_y').value = this.scrollTop;\">"); 

InBlock.gif                                } 

InBlock.gif                                
else 

InBlock.gif                                { 

InBlock.gif                                        writer.Write(
">"); 

InBlock.gif                                } 

InBlock.gif                        } 

InBlock.gif 

InBlock.gif                        
base.Render(writer); 

InBlock.gif 

InBlock.gif                        
// </div> 结束 

InBlock.gif                        
if (!FixRowCol.TableWidth.IsEmpty || !FixRowCol.TableHeight.IsEmpty) 

InBlock.gif                        { 

InBlock.gif                                writer.Write(
"</div>"); 

InBlock.gif                        } 

InBlock.gif                }
 
8、获取存储了滚条位置信息的HiddenField的值
InBlock.gif
void IPostBackDataHandler.RaisePostDataChangedEvent() 

InBlock.gif                { 

InBlock.gif 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
bool IPostBackDataHandler.LoadPostData(
string postDataKey, NameValueCollection postCollection) 

InBlock.gif                { 

InBlock.gif                        
// 获取两个保存了 固定行、列后 的GridView滚动条的位置信息 

InBlock.gif                        _yy_SmartGridView_x = String.IsNullOrEmpty(postCollection[
"yy_SmartGridView_x"]) ? 0 : Convert.ToInt32(postCollection[
"yy_SmartGridView_x"]); 

InBlock.gif                        _yy_SmartGridView_y = String.IsNullOrEmpty(postCollection[
"yy_SmartGridView_y"]) ? 0 : Convert.ToInt32(postCollection[
"yy_SmartGridView_y"]); 

InBlock.gif 

InBlock.gif                        
return 
false

InBlock.gif                }
 
控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置其FixRowCol下的7个属性即可。IsFixHeader是固定表头否?;IsFixPager是固定分页行否?;FixRowIndices是需要固定的行的索引(用逗号“,”分隔);FixColumnIndices是需要固定的列的索引(用逗号“,”分隔);TableWidth是表格的宽度;TableHeight是表格的高度;EnableScrollState为是否保持滚动条的状态
ObjData.cs
InBlock.gif
using System; 

InBlock.gif
using System.Data; 

InBlock.gif
using System.Configuration; 

InBlock.gif
using System.Web; 

InBlock.gif
using System.Web.Security; 

InBlock.gif
using System.Web.UI; 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI.WebControls.WebParts; 

InBlock.gif
using System.Web.UI.HtmlControls; 

InBlock.gif 

InBlock.gif
using System.ComponentModel; 

InBlock.gif 

/// <summary> 

/// OjbData 的摘要说明 

/// </summary> 

InBlock.gif
public 
class OjbData 

InBlock.gif

InBlock.gif        
public OjbData() 

InBlock.gif        { 

InBlock.gif                
// 

InBlock.gif                
// TODO: 在此处添加构造函数逻辑 

InBlock.gif                
// 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        [DataObjectMethod(DataObjectMethodType.Select, 
true)] 

InBlock.gif        
public DataTable Select() 

InBlock.gif        { 

InBlock.gif                DataTable dt = 
new DataTable(); 

InBlock.gif                dt.Columns.Add(
"no"
typeof(
string)); 

InBlock.gif                dt.Columns.Add(
"name"
typeof(
string)); 

InBlock.gif 

InBlock.gif                
for (
int i = 0; i < 30; i++) 

InBlock.gif                { 

InBlock.gif                        DataRow dr = dt.NewRow(); 

InBlock.gif                        dr[0] = 
"no" + i.ToString().PadLeft(2, '0'); 

InBlock.gif                        dr[1] = 
"name" + i.ToString().PadLeft(2, '0'); 

InBlock.gif 

InBlock.gif                        dt.Rows.Add(dr); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
return dt; 

InBlock.gif        } 

InBlock.gif}
 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head id="Head1" runat="server"> 

        <title>SmartGridView测试</title> 

</head> 

<body> 

        <form id="form1" runat="server"> 

                <yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False" 

                        DataSourceID="ObjectDataSource1" Width="1000px"> 

                        <Columns> 

                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" /> 

                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" /> 

                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" /> 

                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" /> 

                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" /> 

                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" /> 

                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" /> 

                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" /> 

                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" /> 

                                <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" /> 

                        </Columns> 

                        <FixRowCol FixColumnIndices="0,1" FixRowIndices="0" IsFixHeader="True" TableHeight="300px" 

                                TableWidth="300px" EnableScrollState="true" /> 

                </yyc:SmartGridView> 

                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 

                        TypeName="OjbData"></asp:ObjectDataSource> 

        </form> 

</body> 

</html>
 
InBlock.gif
/*测试版的实现 结束*/
 

OK
     本文转自webabcd 51CTO博客,原文链接http://blog.51cto.com/webabcd/345511
:,如需转载请自行联系原作者