中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > Web开发 > Asp > 综合文章
asp.net中合并DataGrid行
作者:未知 时间:2004-10-09 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无

例如:

序号            名称      计量单位
一、工业经济    aaa       万元
一、工业经济    bbb       个

合并为:

序号            名称      计量单位
一、工业经济    aaa       万元
                bbb       个
只是将第一列的多行合并为一行,怎么实现?谢谢大家了先!

 

实现方法:
把笨狼哥的改成这样试试:
在.aspx页面,<asp:datagrid>中用
OnPreRender="fDGrid_PreRender">

在.cs文件:

 //合并相同的单元格
  public void fDGrid_PreRender(object sender, System.EventArgs e)
  {
   if(this.fDGrid.Items.Count <=1)
   {
    return;
   }
   col=0;
    TableCell oldtc = this.fDGrid.Items[0].Cells[col];
    for(int i=1;i<this.fDGrid.Items.Count;i++)
    {
     TableCell tc = this.fDGrid.Items[i].Cells[col];
     if(tc.Text == oldtc.Text)
     {
      tc.Visible = false;
      if(oldtc.RowSpan == 0)
      {
       oldtc.RowSpan = 1;
      }
      oldtc.RowSpan = oldtc.RowSpan +1;
      oldtc.VerticalAlign = VerticalAlign.Middle;
     }
     else
     {
      oldtc = tc;
     }
    }
  }

当然,还可以用ItemDataBound事件来处理。具体细节如下

在.cs文件中的
InitializeComponent方法中加入:
  
 this.dgContacts.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgContacts_ItemDataBound);

在.cs文件中的Page_Load中加入:
if (!Page.IsPostBack )
{
lastIndex=0;
}

其中dgContacts为DataGrid的名字

再在 .cs文件中加入下面的代码:
         int lastIndex;
 protected void dgContacts_ItemDataBound(object source,
   System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    string isManager = (string)DataBinder.Eval(e.Item.DataItem, "序号");
    int inn=e.Item.ItemIndex;
    TableCell c;
                                     int col=0;
    if (inn>0)
    {
     if(dgContacts.Items[lastIndex].Cells[col].Text==isManager)
     {
      
      c=new TableCell();
      c=e.Item.Cells[col];
      if(dgContacts.Items[lastIndex].Cells[col].RowSpan==0)
           dgContacts.Items[lastIndex].Cells[col].RowSpan=1;

      dgContacts.Items[lastIndex].Cells[col].RowSpan+=1;
      
      Response.Write(dgContacts.Items[lastIndex].Cells[col].RowSpan);
      e.Item.Cells.Remove(c);
      
     }
     else
     {
      e.Item.Cells[col].Text=isManager;
      lastIndex=e.Item.ItemIndex;
     }
    }
    else
    {
     e.Item.Cells[col].Text=isManager;
    }
    
   }
  }

两种方法都可以,但是还是第一中方法好,通用性也强,第二种方法如果稍加修改,应该也可以。可能还有其他方法。具体用那种方法不重要,重要的是如何灵活应用基本的知识解决复杂的问题。

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有