62,072
社区成员
发帖
与我相关
我的任务
分享
int row = 0;
for (int i = 1; i < gv.Rows.Count; i++)
{
if (gv.Rows[i].Cells[0].Text == gv.Rows[i - 1].Cells[0].Text)
{
//if (gv.Rows[row].Cells[1].RowSpan == 0)
//{
// gv.Rows[row].Cells[0].RowSpan++;
//}
gv.Rows[row].Cells[0].RowSpan++;
gv.Rows[i].Cells[0].Visible = false;
}
else
{
gv.Rows[row].Cells[0].RowSpan++;
row = i;
}
}
gv.Rows[row].Cells[0].RowSpan++;
row = 0;
for (int i = 1; i < gv.Rows.Count; i++)
{
if (gv.Rows[i].Cells[1].Text == gv.Rows[i - 1].Cells[1].Text && gv.Rows[i].Cells[0].RowSpan == 0 && gv.Rows[i].Cells[0].Visible == false)
{
//if (gv.Rows[row].Cells[1].RowSpan == 0)
//{
// gv.Rows[row].Cells[0].RowSpan++;
//}
gv.Rows[row].Cells[1].RowSpan++;
gv.Rows[i].Cells[1].Visible = false;
}
else if (gv.Rows[i].Cells[1].Text == gv.Rows[i - 1].Cells[1].Text && gv.Rows[i].Cells[0].RowSpan > 0 && gv.Rows[i].Cells[0].Visible == true)
{
gv.Rows[row].Cells[1].RowSpan++;
row = i;
}
else
{
gv.Rows[row].Cells[1].RowSpan++;
row = i;
}
}
gv.Rows[row].Cells[1].RowSpan++;
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gv" runat="server" onprerender="gv_PreRender"></asp:GridView>
<asp:GridView ID="GridView1" runat="server" onprerender="GridView1_PreRender"></asp:GridView>
</form>
</body>
</html>
int mergeCount = 2; //前两列合并
private DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(String)));
dt.Columns.Add(new DataColumn("Sex", typeof(String)));
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = "aa";
dr[2] = "男";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 1;
dr[1] = "aa";
dr[2] = "男";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 1;
dr[1] = "cc";
dr[2] = "女";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 2;
dr[1] = "cc";
dr[2] = "男";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 2;
dr[1] = "dd";
dr[2] = "女";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 3;
dr[1] = "dd";
dr[2] = "女";
dt.Rows.Add(dr);
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gv.DataSource = GetDataTable();
gv.DataBind();
GridView1.DataSource = GetDataTable();
GridView1.DataBind();
}
}
protected void gv_PreRender(object sender, EventArgs e)
{
for (int rowIndex = gv.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gv.Rows[rowIndex];
GridViewRow previousRow = gv.Rows[rowIndex + 1];
for (int i = 0; i < row.Cells.Count && i < mergeCount; 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 GridView1_PreRender(object sender, EventArgs e)
{
for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = GridView1.Rows[rowIndex];
GridViewRow previousRow = GridView1.Rows[rowIndex + 1];
if (row.Cells[0].Text == previousRow.Cells[0].Text)
{
row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 :
previousRow.Cells[0].RowSpan + 1;
previousRow.Cells[0].Visible = false;
}
}
for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = GridView1.Rows[rowIndex];
GridViewRow previousRow = GridView1.Rows[rowIndex + 1];
if (row.Cells[1].Text == previousRow.Cells[1].Text)
{
if (!previousRow.Cells[0].Visible)
{
row.Cells[1].RowSpan = previousRow.Cells[1].RowSpan < 2 ? 2 :
previousRow.Cells[1].RowSpan + 1;
previousRow.Cells[1].Visible = false;
}
}
}
}