datalist如何合并相同内容的item.
原内容:
小李 001
小李 002
小李 003合并后:
001
小李 002
003
原内容:
小李 001
小李 002
小李 003合并后:
001
小李 002
003
解决方案 »
- controls更新后覆盖了原来显示在他上面的层
- MSSQL数据库被插入经常被插入类似的字符title.script .http://s.see9.us/s.js,如何解决,先谢了!
- 技术探讨,高手进,顶着有分~~~
- [急!提问]ASP.NET下的用户操作控制和样式解决
- 诚心请教:数据库连接的异常中断
- DataGrid绑定RadioButtonList
- textbox获取焦点同时触发一个刷新事件!
- 急问:radiobutton的问题!!!!!
- 这个同时搜索两个表中的内容的SQL语句正确吗?
- 如何在运行asp.net程序时,让crystal report(带参数)的参数输入对话框弹出来?
- 整个fileup控件的传值
- 弹出页面
只是将第一列的多行合并为一行, 实现方法:
在.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;
}
}
}两种方法都可以,但是还是第一中方法好,通用性也强,第二种方法如果稍加修改,应该也可以。可能还有其他方法。具体用那种方法不重要,重要的是如何灵活应用基本的知识解决复杂的问题。本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20081109/2750.html
EmptyDataText="没有数据" AllowPaging="True" AutoGenerateColumns="False"
BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px" Font-Size="12pt" >
<Columns>
<asp:BoundField DataField="AuthorId" HeaderText="作者ID" SortExpression="AuthorId" /><asp:BoundField DataField="Title" HeaderText="文章标题" SortExpression="Title" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="PubDate" HeaderText="日期" SortExpression="PubDate" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Clicks" HeaderText="点击率" SortExpression="Clicks" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
</asp:GridView>
protected void BindGridView()
{
DataSet ds = new DataSet();
ds = csdn.GetList("Articles", "convert(varchar(10),authorid) authorid,title,pubdate,clicks", " order by authorid");
string str = string.Empty;
if (ds != null)
{
if (ds.Tables[0].Rows.Count != 0)
{
for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
{
if (i == 1)
{
str = ds.Tables[0].Rows[0][0].ToString(); //记下第一行第一列的名称
}
if (ds.Tables[0].Rows[i][0].ToString() == str)
{
ds.Tables[0].Rows[i][0] = " "; //相同则为空
}
else
{
str = ds.Tables[0].Rows[i][0].ToString(); //不同则记下新名称
}
}
}
grdArticle.DataSource = ds.Tables[0].DefaultView;
grdArticle.DataBind();
}
}