iT邦幫忙

0

[GridView] ASP.NET Merge Cell -C#

  • 分享至 

  • xImage
  •  

一樣的Cell合併成一個
本來
https://ithelp.ithome.com.tw/upload/images/20180803/2010676448q5J16Pd8.png

目標
https://ithelp.ithome.com.tw/upload/images/20180803/201067640U2hdHsdZI.png

開始

public bool isSearch = false;

在bind之前將isSearch=true;

拉一個GridView ID為GridView1,在事件PreRender,加入以下code


 protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if (isSearch == false) return;
            isSearch = false;
 

                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;
                    }
                }
            }
        } 
  

如果col是樣板,就要用findeControl取值
```
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (isSearch == false) return;
isSearch = false;

        foreach (GridViewRow gvR in this.GridView1.Rows)
        {
            for (int i = gvR.DataItemIndex + 1; i < this.GridView1.Rows.Count; i++)                
            {
                string x1 =  ((Label)gvR.FindControl("col1")).Text;
                string x2 = ((Label)this.GridView1.Rows[i].FindControl("col1")).Text;

                //與下一列欄位值相等;
                if (x1== x2)
                {
                    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;
                }
            }
        }
    }

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言