出自ProgWiki
用途
- 例如,判斷第1個欄位內容是否重複,將第1~3個欄位做垂直合併。(常用於Select時使用Right Join,導致部分的欄位具有相同的值)
程式碼範例
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);
}
}
}
}