固定表头,加滚动条的datagrid

将datagrid放入一个层中

在html中加入

<style>.CornerStyle {
 BORDER-RIGHT: buttonshadow 1px solid; BORDER-TOP: #ffffff 1px solid; Z-INDEX: 1; BACKGROUND: #33ccff; ; LEFT: expression(document.getElementById('c').scrollLeft); BORDER-LEFT: #ffffff 1px solid; WIDTH: 100px; LINE-HEIGHT: 22px; BORDER-BOTTOM: buttonshadow 1px solid; POSITION: relative; ; TOP: expression(document.getElementById('c').scrollTop); TEXT-ALIGN: center
}
.HeaderItemStyle {
 BORDER-RIGHT: buttonshadow 1px solid; BORDER-TOP: #ffffff 1px solid; BACKGROUND: #33ccff; BORDER-LEFT: #ffffff 1px solid; WIDTH: 220px; WORD-BREAK: break-all; LINE-HEIGHT: 20px; BORDER-BOTTOM: buttonshadow 1px solid; WHITE-SPACE: nowrap; POSITION: relative; ; TOP: expression(document.getElementById('c').scrollTop); TEXT-OVERFLOW: ellipsis; TEXT-ALIGN: center
}
.ContentStyle {
 BORDER-RIGHT: buttonshadow 1px solid; BORDER-TOP: buttonhighlight 1px solid; ; LEFT: expression(document.getElementById('c').scrollLeft); BORDER-LEFT: buttonhighlight 1px solid; WIDTH: 30px; CURSOR: default; WORD-BREAK: break-all; BORDER-BOTTOM: buttonshadow 1px solid; WHITE-SPACE: nowrap; POSITION: relative; TEXT-OVERFLOW: ellipsis; BACKGROUND-COLOR: #fffacd; TEXT-ALIGN: center
}
.ItemStyle {
 BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 1px; PADDING-LEFT: 1px; Z-INDEX: -2; PADDING-BOTTOM: 1px; WIDTH: 220px; WORD-BREAK: break-all; LINE-HEIGHT: 20px; PADDING-TOP: 2px; BORDER-BOTTOM: #cccccc 1px solid; WHITE-SPACE: pre; TEXT-OVERFLOW: ellipsis; TEXT-ALIGN: left
}
.MainDivStyle {
 BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #cccccc 1px solid; MARGIN-LEFT: 0px; OVERFLOW: auto; BORDER-LEFT: #cccccc 1px solid; WIDTH: 400px; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 300px
}
 </style>    

在InitializeComponent()中加入

 this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.MyDataGrid_OnItemDataBound);

在cs中加入

public void MyDataGrid_OnItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if(e.Item.ItemType == ListItemType.Header)
   {
    //  e.Item.Attributes.Add("style", "position:relative; top:expression(this.offsetParent.scrollTop);");
    e.Item.Cells[0].Attributes.Add("class", "CornerStyle");
    for(int i = 1; i<e.Item.Cells.Count;i++)
     e.Item.Cells[i].Attributes.Add("class", "HeaderItemStyle");
   }
   if((e.Item.ItemType == ListItemType.Item) ||(e.Item.ItemType == ListItemType.AlternatingItem)||(e.Item.ItemType == ListItemType.EditItem))
   {
    //e.Item.Cells[3].Visible=false;
    //e.Item.Cells[0].Attributes.Add("style", " LEFT: expression(document.getElementById('c').scrollLeft); POSITION: relative;border-bottom:0px;border-right:0px;background:#ccccff;z-index:102");
    e.Item.Cells[0].Attributes.Add("class", "ContentStyle");
    for(int i = 1; i<e.Item.Cells.Count;i++)
     e.Item.Cells[i].Attributes.Add("class", "ItemStyle");

   }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值