GridView中设置跨行
来源:互联网 发布:55寸液晶电视推荐 知乎 编辑:程序博客网 时间:2024/04/30 08:01
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 {//重新把标签设置为falsecheck = true;//设置要快的行this.GridView1.Rows[rowIndex].Cells[1].RowSpan = rowSpan;//把行数设置默认值rowSpan = 1;}}else {//值相等就设置标签truecheck = 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;}}}}