iT邦幫忙

0

ASP.NET C# GridView Merge Cell問題 _已解決

各位前輩好,在 GridView Merge Cell 遇到的問想請教各位前輩
原始資料如下圖https://ithelp.ithome.com.tw/upload/images/20180330/20107444f4PcFrEMK0.jpg
Merge 完如下圖
https://ithelp.ithome.com.tw/upload/images/20180330/20107444rzuvAc79BP.png
第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);
      }
   }
}

2 個回答

0
圓頭人
iT邦新手 2 級 ‧ 2018-08-22 11:06:18
最佳解答

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;
                    }
                }
            }
        } 
WilliamHuang
iT邦研究生 1 級 ‧ 2018-03-30 14:38:55
【**此則訊息已被站方移除**】

我要發表回答

立即登入回答