GridView中合并重复值

http://www.codeproject.com/KB/webforms/MergeGridViewCells.aspx
 
public class GridDecorator
{
    public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (row.Cells[i].Text == previousRow.Cells[i].Text)
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
                                           previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }
}
protected void gridView_PreRender(object sender, EventArgs e)
{
    GridDecorator.MergeRows(gridView);
}

public static void JoinRows(GridView gvList) { ///合并GridView重复的职务分类 int rowspan = 1; int count = gvList.Rows.Count; //为总计的时候GridViewRow 设置不能反查 GridViewRow currentgvr; for (int j = gvList.Rows.Count - 1; j >= 0; j--) { currentgvr = gvList.Rows[j]; if (currentgvr.Cells[0].Text == "总计") { foreach (Control cols in currentgvr.Controls) { foreach (Control col in cols.Controls) { if (col.GetType().Name == typeof(LinkButton).Name) { col.Visible = false; } if (col.GetType().Name == typeof(Label).Name) { col.Visible = true; } } } } } for (int i = 0; i < count; i++) { currentgvr = gvList.Rows[i]; currentgvr.Cells[0].Visible = false; if (i == count - 1) { gvList.Rows[i + 1 - rowspan].Cells[0].Visible = true; gvList.Rows[i + 1 - rowspan].Cells[0].RowSpan = rowspan; break; } GridViewRow gvrNext = gvList.Rows[i + 1]; if (currentgvr.Cells[0].Text == gvrNext.Cells[0].Text) { rowspan++; } else { gvList.Rows[i + 1 - rowspan].Cells[0].Visible = true; gvList.Rows[i + 1 - rowspan].Cells[0].RowSpan = rowspan; rowspan = 1; } } } Class2 cl2 = new Class2(); string temp = ""; for (int i = 0; i < GridView1.Rows.Count; i++) { if (((TextBox)GridView1.Rows[i].FindControl("textbox1")).Text.ToString().Trim() == "") { Response.Write(""); return; } } for (int ii = 0; ii <= GridView1.Rows.Count - 1; ii++) { //cl2.updatefolder(string user, string name, string nameid) cl2.updatefolder(id, ((TextBox)GridView1.Rows[ii].FindControl("textbox1")).Text.ToString().Trim(),GridView1.DataKeys[GridView1.Rows[ii].RowIndex].Value.ToString()); // } } } this.GridView1.DataSource = cl2.favoriteFolder(id); this.GridView1.DataKeyNames = new string[] { "folderid" }; this.GridView1.Columns[0].Visible = false; this.GridView1.DataBind();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值