概述
#region 合并单元格 合并某一行的所有列
/// <summary>
/// 合并GridView中某行相同信息的行(单元格)
/// </summary>
/// <param name="GridView1">GridView对象</param>
/// <param name="cellNum">需要合并的行</param>
public static void GroupRow(GridView GridView1, int rows)
{
TableCell oldTc = GridView1.Rows[rows].Cells[0];
for (int i = 1; i < GridView1.Rows[rows].Cells.Count; i++)
{
TableCell tc = GridView1.Rows[rows].Cells[i]; //Cells[0]就是你要合并的列
if (oldTc.Text == tc.Text)
{
tc.Visible = false;
if (oldTc.ColumnSpan == 0)
{
oldTc.ColumnSpan = 1;
}
oldTc.ColumnSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldTc = tc;
}
}
}
#endregion
#region 合并单元格 合并一行中的几列
/// <summary>
/// 合并单元格 合并一行中的几列
/// </summary>
/// <param name="GridView1">GridView ID</param>
/// <param name="rows">行</param>
/// <param name="sCol">开始列</param>
/// <param name="eCol">结束列</param>
public static void GroupRow(GridView GridView1, int rows,int sCol,int eCol)
{
TableCell oldTc = GridView1.Rows[rows].Cells[sCol];
for (int i = 1; i < eCol - sCol; i++)
{
TableCell tc = GridView1.Rows[rows].Cells[i + sCol]; //Cells[0]就是你要合并的列
tc.Visible = false;
if (oldTc.ColumnSpan == 0)
{
oldTc.ColumnSpan = 1;
}
oldTc.ColumnSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
}
#endregion
#region 合并单元格 合并某一列所有行
/// <summary>
/// 合并GridView中某列相同信息的行(单元格)
/// </summary>
/// <param name="GridView1"></param>
/// <param name="cellNum"></param>
public static void GroupCol(GridView GridView1, int cols)
{
if (GridView1.Rows.Count < 1 || cols > GridView1.Rows[0].Cells.Count - 1)
{
return;
}
TableCell oldTc = GridView1.Rows[0].Cells[cols];
for (int i = 1; i < GridView1.Rows.Count; i++)
{
TableCell tc = GridView1.Rows[i].Cells[cols];
if (oldTc.Text == tc.Text)
{
tc.Visible = false;
if (oldTc.RowSpan == 0)
{
oldTc.RowSpan = 1;
}
oldTc.RowSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldTc = tc;
}
}
}
#endregion
#region 合并单元格 合并某一列中的某些行
/// <summary>
/// 合并单元格 合并某一列中的某些行
/// </summary>
/// <param name="GridView1">GridView ID</param>
/// <param name="cellNum">列</param>
/// <param name="sRow">开始行</param>
/// <param name="eRow">结束列</param>
public static void GroupCol(GridView GridView1, int cols,int sRow,int eRow)
{
if (GridView1.Rows.Count < 1 || cols > GridView1.Columns.Count - 1)
{
return;
}
TableCell oldTc = GridView1.Rows[sRow].Cells[cols];
for (int i = 1; i < eRow - sRow; i++)
{
TableCell tc = GridView1.Rows[sRow + i].Cells[cols];
tc.Visible = false;
if (oldTc.RowSpan == 0)
{
oldTc.RowSpan = 1;
}
oldTc.RowSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
}
#endregion
实例:实现效果如下:
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 XNCJWC.BLL;
using XNCJWC.Model;
using XNCJWC.Utility;
namespace XNCJWC.Web
{
public partial class GradeManage_GradeQueryPersonal : System.Web.UI.Page
{
private XNCJWC.BLL.Common commonBll = new XNCJWC.BLL.Common();//公共业务逻辑
private xscjBLL xscjBll = new xscjBLL();//学生成绩业务逻辑
string strWhere = "";
double sum1 = 0;
double sum2 = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//绑定学年下拉框
commonBll.BindYearDownList(ddlstXn);
//绑定学期下拉框
commonBll.BindTermDownList(ddlstXq);
//初始化学年
this.ddlstXn.SelectedValue = Utility.Common.GetCurrentYear();
//初始化学期
this.ddlstXq.SelectedValue = Utility.Common.GetCurrentTerm();
}
}
/// <summary>
/// 查询事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnQuery_Click(object sender, EventArgs e)
{
strWhere = GetQueryWhere();
//绑定jv
strWhere += " order by xn,xq";
Query(strWhere);
}
/// <summary>
/// 查询
/// </summary>
private void Query(string strWhere)
{
DataTable dt = xscjBll.GetList(strWhere).Tables[0];
if (dt.Rows.Count > 0)
{
this.Panel1.Visible = true;
this.lblTitle.Text = "学生个人成绩单";
this.lblInfo.Text = "学号: " + dt.Rows[0]["xh"].ToString() + " 姓名: " + dt.Rows[0]["xm"].ToString();
this.gvGradeQuery.DataSource = dt;
this.gvGradeQuery.DataBind();
GroupRows(gvGradeQuery, 1);
GroupRows(gvGradeQuery, 0);
}
else
{
ScriptManager.RegisterStartupScript(this.gvGradeQuery, gvGradeQuery.GetType(), "NoRecord", "alert('没有该学生该学年学期的课程成绩!');", true);
}
}
/// <summary>
/// 获取查询条件
/// </summary>
/// <returns></returns>
private string GetQueryWhere()
{
string strWhere = "";
if (this.ddlstXn.SelectedValue != null && !this.ddlstXn.SelectedValue.Equals(""))
strWhere = " xn ='" + this.ddlstXn.SelectedValue + "' and ";
if (this.ddlstXq.SelectedValue != null && !this.ddlstXq.SelectedValue.Equals(""))
strWhere += " xq ='" + this.ddlstXq.SelectedValue + "' and ";
if (txtStuXh.Text.Trim().Length > 0 && txtStuXh.Text != null)
strWhere += " xh='" + this.txtStuXh.Text.Trim() + "' and";
if (strWhere.Trim().Length > 0)
strWhere = strWhere.Substring(0, strWhere.LastIndexOf("and"));
return strWhere;
}
/// <summary>
/// 设置学期显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvGradeQuery_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
// e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#C0FAFF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
if (!e.Row.Cells[7].Text.Equals("")&&Convert.ToDecimal(e.Row.Cells[7].Text) < 60 )
e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
switch (e.Row.Cells[1].Text.Trim())
{
case "1":
e.Row.Cells[1].Text ="春季学期";
e.Row.Cells[0].Text = (Convert.ToInt32(e.Row.Cells[0].Text) - 1).ToString() + "-" + e.Row.Cells[0].Text;
break;
case "2":
e.Row.Cells[0].Text = (Convert.ToInt32(e.Row.Cells[0].Text) - 1).ToString() + "-" + e.Row.Cells[0].Text;
e.Row.Cells[1].Text = "夏季学期";
break;
case "3":
e.Row.Cells[1].Text = "秋季学期";
e.Row.Cells[0].Text = e.Row.Cells[0].Text + "-" + (Convert.ToInt32(e.Row.Cells[0].Text) + 1).ToString();
break;
case "4":
e.Row.Cells[0].Text = e.Row.Cells[0].Text + "-" + (Convert.ToInt32(e.Row.Cells[0].Text) + 1).ToString();
e.Row.Cells[1].Text = "冬季学期";
break;
}
if (e.Row.Cells[5].Text == "0")
e.Row.Cells[5].Text = "";
if (e.Row.Cells[6].Text == "0")
e.Row.Cells[6].Text = "";
if (!e.Row.Cells[3].Text.Equals(""))
{
sum1 += Convert.ToDouble(e.Row.Cells[3].Text);
}
if (!e.Row.Cells[8].Text.Equals(""))
{
sum2 += Convert.ToDouble(e.Row.Cells[8].Text);
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "总计:";
e.Row.Cells[3].Text = sum1.ToString();
e.Row.Cells[8].Text = sum2.ToString();
}
}
/// <summary>
/// 合并GridView中某列相同信息的行(单元格)
/// </summary>
/// <param name="GridView1">GridView</param>
/// <param name="cellNum">第几列</param>
public static void GroupRows(GridView GridView1, int cellNum)
{
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 (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
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;
}
}
}
}
/// <summary>
/// 查询学生所有成绩
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAllGrade_Click(object sender, EventArgs e)
{
if (txtStuXh.Text.Trim().Length > 0 && txtStuXh.Text != null)
{
strWhere = " xh='" + this.txtStuXh.Text.Trim() + "'";
strWhere += " order by xn,xq";
Query(strWhere);
}
}
}
}
转载于:https://www.cnblogs.com/hubcarl/archive/2009/05/10/1453489.html
最后
以上就是靓丽鸵鸟为你收集整理的Gridview合并单元格的全部内容,希望文章能够帮你解决Gridview合并单元格所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复