日期:2014-05-18  浏览次数:20382 次

GridView 中有一列跨过几行,应该如何设置?
我这个GridView   总共有四列,其中第一、二、四列都绑定在同一个表的三个字段上,大概有十几个数据项(即十几行);而第三列是绑定在另外一个表的某个字段上的一个数据项上,就仅仅一行,为了照顾格式,要使这个项在上下方向跨越所有行,让它一列从上到底,请问各路豪杰,应该怎么办?

------解决方案--------------------
绑定事件里面去合并相同的行!
------解决方案--------------------
合并单元格,以合并表头为例:后台
/// <summary>
/// 重新生成表头,合并删除与操作行
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//判断创建的行是不是标题行
if (e.Row.RowType == DataControlRowType.Header )
{
TableCellCollection tcl = e.Row.Cells;
//清除自动生成的表头
tcl.Clear();

//添加新的表头
tcl.Add(new TableHeaderCell());
tcl[0].Style.Add( "width ", "10% ");
tcl[0].Text = "员工编号 ";

tcl.Add(new TableHeaderCell());
tcl[1].Style.Add( "width ", "6% ");
tcl[1].Text = "员工姓名 ";

tcl.Add(new TableHeaderCell());
tcl[2].Style.Add( "width ", "20% ");
tcl[2].Text = "项目名称 ";

tcl.Add(new TableHeaderCell());
tcl[3].Style.Add( "width ", "8% ");
tcl[3].Text = "项目角色 ";

tcl.Add(new TableHeaderCell());
tcl[4].Style.Add( "width ", "8% ");
tcl[4].Text = "参加项目日期 ";

tcl.Add(new TableHeaderCell());
tcl[5].Style.Add( "width ", "8% ");
tcl[5].Text = "离开项目日期 ";

tcl.Add(new TableHeaderCell());
tcl[6].Style.Add( "width ", "27% ");
tcl[6].Text = "备注 ";

tcl.Add(new TableHeaderCell());
tcl[7].Style.Add( "width ", "13% ");
tcl[7].ColumnSpan = 2;
tcl[7].Style.Add( "text-align ", "center ");
tcl[7].Text = "操作 ";
}
}
前台在GridView中添加动作事件 OnRowCreated= "GridView1_RowCreated "
要是实现,应该在 if (e.Row.RowType == DataControlRowType.Header )
去看看找到数据行来合并也行的

------解决方案--------------------
因为只有绑定完成之后,才能获取行数(合并数),
故在 GridView 的 DataBound 事件中处理

protected void GridView1_DataBound(object sender, EventArgs e)
{
// 第一行三列跨行
GridView1.Rows[0].Cells[2].RowSpan = GridView1.Rows.Count;
// 其他行第三列移出
for (int i = 1; i < GridView1.Rows.Count; i++) {
GridView1.Rows[i].Cells.RemoveAt(2);
}
}