例如:
序号 名称 计量单位 一、工业经济 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; } } }
两种方法都可以,但是还是第一中方法好,通用性也强,第二种方法如果稍加修改,应该也可以。可能还有其他方法。具体用那种方法不重要,重要的是如何灵活应用基本的知识解决复杂的问题。
[C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]VB.NET中关于DataGrid颜色的自定义。
|