GridView每幾行增加一空行的方法
出處:http://www.zdbase.com/content/detail.aspx?OID=F522C044-3DD3-4586-9641-0DA25152F93E
經常見到文章列表中,每隔五行或十行下面有一空行,這樣當閱讀網站內容時,會不覺得太壓抑,其實這個GridView也可以做到。
1.首先GridView要定義OnRowDataBound事件,例如:OnRowDataBound="gv_RowDataBound"
2.完善gv_RowDataBound方法體內容:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex > 0 && (e.Row.RowIndex+1) % 5 == 0)
{
GridViewRow newRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
newRow.Cells.Add(new TableCell());
newRow.Cells[0].ColumnSpan = e.Row.Cells.Count;
newRow.Cells[0].Text = " ";
this.gv.Controls[0].Controls.Add(newRow);
}
}
這裏以5行添加一空行爲例,如果要改成10行或其他,只需更改事件裏的數字就好了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.