将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");
}
}