您的位置:首页 > 其它

纵向合并gridview单元格的两种方法

2007-05-19 15:26 381 查看
比如说有这样一组数据用gridview展示出来是这样

namenum
a0
a1
a2
a3
a4
b5
b6
b7
b8
b9
b10
但想把相同的name放在一个单元格里,就像这样

namenum
a0
1
2
3
4
b5
6
7
8
9
10
应该怎么做呢,本人总结出两种方法,其实原理都是一样的,就是逐行判断要合并的单元格里的值是否和上一行的相同,要是相同的话就合并,不同的话就接着判断。只不过是判断的时间不同,代码也不相同。一是在gridview的RowDataBound事件中判断,就是在每一行数据绑定完成时进行上面的操作,需要一个全局变量来记录每次单元格变化的头一行。另一种是在gridview的DataBound事件中判断,就是在gridview完全绑定好以后进行上面的操作。

看下面的代码
页面:




<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title>无标题页</title>


</head>


<body>


<form id="form1" runat="server">


<div>


<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound">


</asp:GridView>




</div>


</form>


</body>


</html>



后台代码:


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


using System.Net;




public partial class Default3 : System.Web.UI.Page




...{


protected void Page_Load(object sender, EventArgs e)




...{


GridView1.DataSource = getDataTable();


GridView1.DataBind();


}




private DataTable getDataTable()




...{


DataTable dt = new DataTable();


dt.Columns.Add(new DataColumn("name", typeof(string)));


dt.Columns.Add(new DataColumn("num", typeof(string)));


DataRow dr;


for (int i = 0; i <= 10; i++)




...{


dr = dt.NewRow();


if (i < 5)


dr[0] = "a";


else


dr[0] = "b";


dr[1] = i.ToString();


dt.Rows.Add(dr);


}


return dt;


}




int row = 0;


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)




...{


int rowindex = e.Row.RowIndex;


if (rowindex - 1 < 0) return;


if (e.Row.Cells[0].Text == GridView1.Rows[rowindex - 1].Cells[0].Text)




...{


if (GridView1.Rows[row].Cells[0].RowSpan == 0) GridView1.Rows[row].Cells[0].RowSpan++;


GridView1.Rows[row].Cells[0].RowSpan++;


e.Row.Cells[0].Visible = false;


}


else




...{


row = rowindex;


}


}


protected void GridView1_DataBound(object sender, EventArgs e)




...{


//int row=0;


//for (int i = 1; i < GridView1.Rows.Count; i++)


//{


// if (GridView1.Rows[i].Cells[0].Text == GridView1.Rows[i - 1].Cells[0].Text)


// {


// if (GridView1.Rows[row].Cells[0].RowSpan == 0) GridView1.Rows[row].Cells[0].RowSpan++;


// GridView1.Rows[row].Cells[0].RowSpan++;


// GridView1.Rows[i].Cells[0].Visible = false;


// }


// else


// {


// row = i;


// }


//}


}


}



两种方法都在上面了,大家可以取消掉GridView1_DataBound的注释,把GridView1_RowDataBound里的代码加上注释试一试,效果是一样的。

加上这一行
if (GridView1.Rows[row].Cells[0].RowSpan == 0) GridView1.Rows[row].Cells[0].RowSpan++;

是因为cells的RowSpan属性默认值为0,要做合并的话初始值应该是1,否则就会少合并一行。

两个方法那个效率高也没有具体测试,有兴趣的朋友请试一试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: