如果表中的ID列是自动加1的,如果删除之后,这个行号就没了,在GridView中显示ID列就会少一个,这样看起来很郁闷,所以我就做了一个方法,在在行绑定事件里加上如下代码即可,并且SQL语句里加一个空列,如:
select null as 序号,name,age from test
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
}
鼠标放在行上变颜色:
protected
void
GridView1_RowCreated(
object
sender, GridViewRowEventArgs e)
... {
if (e.Row.RowType == DataControlRowType.DataRow)
...{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//当鼠标移走时还原该行的背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
... {
if (e.Row.RowType == DataControlRowType.DataRow)
...{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//当鼠标移走时还原该行的背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}