在asp.net中,我使用gridview來顯示數據。在gridview中,我一次顯示300條記錄。現在我想在GridView中的每30行之後顯示標題模板嗎?你可以幫我嗎。謝謝。 我這樣寫在gridview_rowdatabound中。如何在gridivew中重複標題模板?
GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.Controls.Add(new Label { Text = "Header" }); //as needed
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(new Label { Text = "Name" }); //as needed
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(new Label { Text = "add" }); //as needed
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(new Label { Text = "Nice" }); //as needed
row.Cells.Add(cell);
Table tbl = (e.Row.Parent as Table);
tbl.Controls.AddAt(e.Row.RowIndex * 3 + 1, row);
當我把tbl.Controls.AddAt(e.Row.RowIndex * 2 + 1,行);當我像這樣放置2個像3個,4個時,它正在工作,那麼它就是連接錯誤。
謝謝所有我解決這個問題:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
{
ViewState["i"] = e.Row.RowIndex;
}
if (Convert.ToInt32(ViewState["i"].ToString()) == e.Row.RowIndex)
{
GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.BackColor = System.Drawing.Color.BurlyWood;
cell.Controls.Add(new Label { Text = "header" }); //as needed
row.Cells.Add(cell);
cell = new TableCell();
cell.BackColor = System.Drawing.Color.BurlyWood;
cell.Controls.Add(new Label { Text = "Name" }); //as needed
row.Cells.Add(cell);
cell = new TableCell();
cell.BackColor = System.Drawing.Color.BurlyWood;
cell.Controls.Add(new Label { Text = "add" }); //as needed
row.Cells.Add(cell);
cell = new TableCell();
cell.BackColor = System.Drawing.Color.BurlyWood;
cell.Controls.Add(new Label { Text = "Nice" }); //as needed
row.Cells.Add(cell);
int cnt = Convert.ToInt32(ViewState["i"].ToString());
Table tbl = (e.Row.Parent as Table);
//tbl.Controls.AddAt(e.Row.RowIndex * cnt , row);
tbl.Controls.AddAt(e.Row.RowIndex, row);
cnt = cnt + 4;
ViewState["i"] = cnt.ToString();
}
}
謝謝你,答覆,我解決這個問題。 –
我把e.row.index放在integr變量中,並檢查條件if(e.row.rowindex == i)然後顯示標題,如果條件我給我賦值,就像i = i + 30,之前我寫了一個條件來確定價值,請檢查我正在更新我的問題,因爲我正在放置代碼。 –