GridView合并列下的行单元格的方法
1.在GridView中要合并單元格,如得到如下效果
??
?? ? ? ? ?可在GridvIew中調的DataBind事件調用下面類中的方法,先在項目的App_Code文件夾下添加下面的類,下面類中寫了兩個方法,一個是模板列的,一個是普通列的,如果您想合并的是模板列,就調用模板列的方法,如果 您想俁并的是普通列就調用普通列的方法:
?? ? ? ? ? ? ? ? ? ? ? ? 上面效果就是在DataBind事件中調用右邊:?UnitCommon.UnitCell( GridView1 , 0 );
類代碼如下:
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// UnitCommon 的摘要說明
/// </summary>
public class UnitCommon
{
?? /// <summary>
?? /// 合并行(普通列)
?? /// </summary>
?? /// <param name=“gv”>所對應的GridView對象</param>
?? /// <param name=“columnIndex”>所對應要合并的列的索引,從0開始</param>
?? public static void UnitCell(GridView gv, int columnIndex)
?? ?{
?? ? ? int i;
?? ? ? string lastType;
?? ? ? int lastCell;
?? ? ? if (gv.Rows.Count > 0)
?? ? ? ?{
?? ? ? ? ? lastType = gv.Rows[0].Cells[columnIndex].Text;
?? ? ? ? ? gv.Rows[0].Cells[columnIndex].RowSpan = 1;
?? ? ? ? ? lastCell = 0;
?? ? ? ? ? for (i = 1; i < gv.Rows.Count; i++)
?? ? ? ? ? ?{
?? ? ? ? ? ? ? if (gv.Rows[i].Cells[columnIndex].Text == lastType)
?? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? gv.Rows[i].Cells[columnIndex].Visible = false;
?? ? ? ? ? ? ? ? ? gv.Rows[lastCell].Cells[columnIndex].RowSpan++;
?? ? ? ? ? ? ? ? }
?? ? ? ? ? ? ? ?else
?? ? ? ? ? ? ? ? {
?? ? ? ? ? ? ? ? ? lastType = gv.Rows[i].Cells[columnIndex].Text;
?? ? ? ? ? ? ? ? ? lastCell = i;
?? ? ? ? ? ? ? ? ? gv.Rows[i].Cells[columnIndex].RowSpan = 1;
?? ? ? ? ? ? ? ? }
?? ? ? ? ? ?}
?? ? ? ?}
?? ?}
?? ?/// <summary>
?? ?/// 合并行(模板列)
?? ?/// </summary>
?? ?/// <param name=“gv”>所對應的GridView對象</param>
?? ?/// <param name=“columnIndex”>所對應要合并的列的索引,從0開始</param>
?? ?/// <param name=“lblName”>模板列里面Lable的Id</param>
?? ?public static void UnitCell(GridView gv, int columnIndex, string lblName)
?? ? {
?? ? ? ?int i;
?? ? ? ?string lastType;
?? ? ? ?int lastCell;
?? ? ? ?if (gv.Rows.Count > 0)
?? ? ? ? {
?? ? ? ? ? lastType = (gv.Rows[0].Cells[columnIndex].FindControl(lblName) as Label).Text;
?? ? ? ? ? gv.Rows[0].Cells[columnIndex].RowSpan = 1;
?? ? ? ? ? lastCell = 0;
?? ? ? ? ? for (i = 1; i < gv.Rows.Count; i++)
?? ? ? ? ? ?{
?? ? ? ? ? ? ?if ((gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text == lastType)
?? ? ? ? ? ? ? {
?? ? ? ? ? ? ? ? gv.Rows[i].Cells[columnIndex].Visible = false;
?? ? ? ? ? ? ? ? gv.Rows[lastCell].Cells[columnIndex].RowSpan++;
?? ? ? ? ? ? ? }
?? ? ? ? ? ? ?else
?? ? ? ? ? ? ? {
?? ? ? ? ? ? ? ? lastType = (gv.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text;
?? ? ? ? ? ? ? ? lastCell = i;
?? ? ? ? ? ? ? ? gv.Rows[i].Cells[columnIndex].RowSpan = 1;
?? ? ? ? ? ? ? }
?? ? ? ? ? ?}
?? ? ? ?}
?? ? }
}
?
轉載于:https://www.cnblogs.com/yingger/archive/2012/08/03/2621324.html
總結
以上是生活随笔為你收集整理的GridView合并列下的行单元格的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1026: [SCOI2009]wind
- 下一篇: xcode 4.3 调试的时候显示汇编,