asp.net中合并DataGrid行

王朝asp·作者佚名  2006-01-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;

}

}

}

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航