protected void GridView1_DataBound(object sender, EventArgs e) { settingRow(this.GridView1, 0); settingRow(this.GridView1, 1); settingRow(this.GridView1, 2); settingRow(this.GridView1, 3); settingRow(this.GridView1, 4); } public void settingRow(GridView gv,int cellsNum) { int i = 0, rowSpanNum = 1; //循环当前GridView中的所有行 while(i<gv.Rows.Count-1){ //获得一行数据 GridViewRow gvr = gv.Rows[i]; //循环当前GridView中的所有行 for (++i;i<gv.Rows.Count; i++) { //获得需要比较的一行数据 GridViewRow gvrNext = gv.Rows[i]; string gvrText = (gvr.Cells[cellsNum].FindControl("lbl"+cellsNum.ToString()) as Label).Text; string gvrNextText = (gvrNext.Cells[cellsNum].FindControl("lbl"+cellsNum.ToString()) as Label).Text; //比较,如果相等则合并 // if (gvr.Cells[cellsNum].Text == gvrNext.Cells[cellsNum].Text) if(gvrText==gvrNextText) { gvrNext.Cells[cellsNum].Visible = false; rowSpanNum++; } else { gvr.Cells[cellsNum].RowSpan = rowSpanNum; rowSpanNum = 1; break; } //如果相等则合并一整行 if(i==gv.Rows.Count-1){ gvr.Cells[cellsNum].RowSpan = rowSpanNum; } } } protected void GridView1_DataBound(object sender, EventArgs e) { //要合并的行数 int rowSpan = 1; //要 int rowIndex = 0; //保存上一个值 string code = null; //标签 bool check = true; //循环 for (int i = 0; i < this.GridView1.Rows.Count; i++) { //取出要比较的值 string txt = this.GridView1.Rows[i].Cells[1].Text; //比较值 if (code != txt) { //不想等就保存下来 code = txt; //值是否相等 if (check) { rowIndex = i; } else { //重新把标签设置为false check = true; //设置要快的行 this.GridView1.Rows[rowIndex].Cells[1].RowSpan = rowSpan; //把行数设置默认值 rowSpan = 1; } } else { //值相等就设置标签true check = false; //把当前单元格隐长 this.GridView1.Rows[i].Cells[1].Visible = false; //跨几行 rowSpan++; //判断是否是最后一行 if (i + 1 == this.GridView1.Rows.Count) { this.GridView1.Rows[rowIndex].Cells[1].RowSpan = rowSpan; } } } }
GridView中设置跨行
最新推荐文章于 2021-05-29 04:55:20 发布