首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>ASP.NET>如何在asp.net中合并DataGrid行?
如何在asp.net中合并DataGrid行?
来源: 发布时间:2008-11-09 发布人: 浏览: 人次   字体: [ ]  

本文要实现的效果如下:

序号            名称      计量单位
一、工业经济    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;
    }    
   }
  }

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


相 关 文 章   发布商链接
·解决asp.net中ReportViewer控件日历...
·asp.net生成高清晰缩略图的方法源码
·asp.net从数据库中读出图片并生成文...
·如何实现在DataList控件中的DropDown...
·实例讲解如何实现DataList控件的翻页...
·如何使用HttpWebRequest的POST取得网...
·异步操作使用HttpWebRequest的POST取...
·编程实现如何检测页面中的404错误
·C#实现将汉字转化为拼音的代码
·C#实现汉字转换为拼音缩写的代码
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·实例讲解ASP.NET实现加密Cook...
·如何实现在DataList控件中的D...
·实例讲解asp.net生成高质量缩
·asp.net适用于IE或FireFox的...
·举例详解C#代码与javaScript...
·asp.net无刷新上传文件
·在Ie中保存图片时出现"800700...
·asp.net文件上传大小限制的控
·asp.net动态生成txt文本文件...
·asp.net实现将Excel文件导入...
·实现多列,带图片的DropDownLi...
·javascript实现TreeView 控件
·ASP.NET配置Word的操作权限
·让FileUpload控件在IE和FireF...
·ASP.NET实现Office文档的分类
热 门 文 章
·asp.net(C#)上传下载及文件管...
·图解asp.net如何用excel做报...
·asp.net无刷新上传文件
·Asp.Net防止刷新重复提交数据...
·asp.net实现将Excel文件导入...
·ASP.NET取得物理路径和虚拟路...
·asp.net中Web.Config配置文件...
·asp.net(c#)生成验证码代码,...
·asp.net页面回传与js调用服务...
·asp.net中DataBinder.Eval的...
·asp.net(C#版)实现登录验证码...
·DataGrid中DropDownList触发S...
·asp.net可输入的下拉框复合控...
·ASP.net 实现批量数据更新或...
·asp.net中常见的几种日历控件...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .