求一合併相同行問題,謝謝!
public void GridViewGroupRows1(GridView GridView2, int cellNum1, int cellNum2)
{
int i = 0,rowSpanNum1 = 1, rowSpanNum2 = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr1 = GridView2.Rows[i];
for (++i; i < GridView2.Rows.Count; i++)
{
GridViewRow gvrNext1 = GridView2.Rows[i];
GridViewRow gvrNext2 = GridView2.Rows[i];
if (gvr1.Cells[cellNum1].Text == gvrNext1.Cells[cellNum1].Text)
{
gvrNext1.Cells[cellNum1].Visible = false;
rowSpanNum1++;
if (gvr1.Cells[cellNum2].Text == gvrNext1.Cells[cellNum2].Text)
{
gvrNext2.Cells[cellNum2].Visible = false;
rowSpanNum2++;
}
}
else
{
gvr1.Cells[cellNum1].RowSpan = rowSpanNum1;
gvr1.Cells[cellNum2].RowSpan = rowSpanNum2;
rowSpanNum2 = 1;
rowSpanNum1 = 1;
break;
}
if (i == GridView2.Rows.Count - 1)
{
gvr1.Cells[cellNum1].RowSpan = rowSpanNum1;
gvr1.Cells[cellNum2].RowSpan = rowSpanNum2;
}
}
}
}
我的要求是第一列中有相同行則合併,如果第一列中有相同行並且第二列也有,則第二列也合併,我寫了一算法有問題,第二列有的合併,有的不合併,不知道為什麼
[解决办法]
一个合并的例子参考一下
<%...@ 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;
// }
//}
}
}
[解决办法]
/// <summary>
/// 合并单元格
/// </summary>
/// <param name= "GridView1 "> 要合并的GridView </param>
/// <param name= "cellNums "> 要合并的列组 </param>
/// <param name= "curIndex "> 当前合并的序列 </param>
/// <param name= "mergeType "> 合并类型 </param>
private static void MergeRows(GridView GridView1, List <int> cellNums, int curIndex,MergeCellValueType mergeType)
{
int cellNum = cellNums[curIndex];
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr = GridView1.Rows[i];
for (++i; i < GridView1.Rows.Count; i++)
{
GridViewRow gvrNext = GridView1.Rows[i];
if (CheckCanMerge(gvr,gvrNext,cellNums,curIndex,mergeType))
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}
if (i == GridView1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}