收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 关于Gridview控件合并某2列相同信息的问题(续)

kukukey 2009-11-11 03:45:33
上一帖的链接在此
http://topic.csdn.net/u/20091111/10/48477ac5-2f55-4a5e-8d23-966548a6f10a.html?seed=445820210&r=61093792#r_61093792
能不能以第一列为首要,第1列的合并为第2列合并的基础。如图cc这个值,既属于A又属于B且相邻在一起,两个值需要以第一列为标准进行区分。请阿非大哥再来看看!有兴趣的也一起来看看,继续拓展下!
如图:

...全文
286 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
有创服务 2009-12-24
  • 打赏
  • 举报
回复
不知道管用不!你们都不用GridView吗?
那你们用什么呢?
kukukey 2009-11-11
  • 打赏
  • 举报
回复
老实的人的那个方法也试过了。也也可以。可以的。达到效果了。当然阿非大哥
研究研究!真不好意思!以后还是需要多举一反三了。
结贴!
caobingyi 2009-11-11
  • 打赏
  • 举报
回复
试试这个,测试过可以用
        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++;


gongsun 2009-11-11
  • 打赏
  • 举报
回复
为什么那么多人都在问gw的问题呢?...
gongsun 2009-11-11
  • 打赏
  • 举报
回复
...
阿非 2009-11-11
  • 打赏
  • 举报
回复

<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;
}

}

}

}
ajaxtop 2009-11-11
  • 打赏
  • 举报
回复
先帮顶

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧