各位前輩好,在 GridView Merge Cell 遇到的問想請教各位前輩
原始資料如下圖
Merge 完如下圖
第38,37筆資料被合併不建了,第33筆資料沒合併到
想請問各位大大程式哪邊要做修正呢??
for (int i = 0; i < this.GridView2.Rows.Count; i++)
{
int i0 = i;
//判斷Cells[0]是否依樣,不一樣就Break
string strCells0 = this.GridView2.Rows[i].Cells[0].Text;
for (int j = i + 1; j < this.GridView2.Rows.Count; j++)
{
if (strCells0 != this.GridView2.Rows[j].Cells[0].Text)
break;
i++;
}
//垂直合併資料列的部份欄位
if (i != i0)
{
string strRowSpan = (i - i0 + 1).ToString();
this.GridView2.Rows[i0].Cells[0].Attributes.Add("rowspan", strRowSpan);
this.GridView2.Rows[i0].Cells[1].Attributes.Add("rowspan", strRowSpan);
this.GridView2.Rows[i0].Cells[2].Attributes.Add("rowspan", strRowSpan);
this.GridView2.Rows[i0].Cells[3].Attributes.Add("rowspan", strRowSpan);
this.GridView2.Rows[i0].Cells[4].Attributes.Add("rowspan", strRowSpan);
this.GridView2.Rows[i0].Cells[5].Attributes.Add("rowspan", strRowSpan);
for (int x = i0 + 1; x <= i; x++)
{
this.GridView2.Rows[x].Cells.RemoveAt(0);
this.GridView2.Rows[x].Cells.RemoveAt(0);
this.GridView2.Rows[x].Cells.RemoveAt(0);
this.GridView2.Rows[x].Cells.RemoveAt(0);
this.GridView2.Rows[x].Cells.RemoveAt(0);
this.GridView2.Rows[x].Cells.RemoveAt(0);
}
}
}
merge相同的
protected void GridView1_PreRender(object sender, EventArgs e)
{
for (int i = gvR.DataItemIndex + 1; i < this.GridView1.Rows.Count; i++)
{
//與下一列欄位值相等;
if (gvR.Cells[0].Text.Trim() == this.GridView1.Rows[i].Cells[0].Text.Trim())
{
gvR.Cells[0].RowSpan = gvR.Cells[0].RowSpan == 0 ? gvR.Cells[0].RowSpan = 2 : gvR.Cells[0].RowSpan += 1;
this.GridView1.Rows[i].Cells[0].Visible = false;
}
else
{
break;
}
}
}
}