GridView重複內容的欄位做垂直合併

出自ProgWiki

跳轉到: 導航, 搜尋

用途

程式碼範例

protected void GridView1_DataBound(object sender, EventArgs e)
{
	for (int i = 0; i < this.GridView1.Rows.Count; i++)
	{
		int i0 = i;
 
		string strCells0 = this.GridView1.Rows[i].Cells[0].Text;
		for (int j = i + 1; j < this.GridView1.Rows.Count; j++)
		{
			if (strCells0 != this.GridView1.Rows[j].Cells[0].Text)
				break;
 
			i++;
		}
 
		//垂直合併資料列的部份欄位
		if (i != i0)
		{
			string strRowSpan = (i-i0+1).ToString();
			this.GridView1.Rows[i0].Cells[0].Attributes.Add("rowspan", strRowSpan);
			this.GridView1.Rows[i0].Cells[1].Attributes.Add("rowspan", strRowSpan);
			this.GridView1.Rows[i0].Cells[2].Attributes.Add("rowspan", strRowSpan);
 
			for (int x = i0 + 1; x <= i; x++)
			{
				this.GridView1.Rows[x].Cells.RemoveAt(0);
				this.GridView1.Rows[x].Cells.RemoveAt(0);
				this.GridView1.Rows[x].Cells.RemoveAt(0);
			}
		}
	}
}
個人工具
名字空間
變換
動作
導航
分類
其他
技術類News或部落格
工具箱