datalist如何合并相同内容的item.
原内容:
小李  001
小李  002
小李  003合并后:
      001
小李  002
      003

解决方案 »

  1.   

    可以参考
    只是将第一列的多行合并为一行, 实现方法:
    在.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
      

  2.   

    可是datalist没有RowSpan的属性..............
      

  3.   

    有个变通方法,先排好序,在第一行中标记小李,接下来若为小李的列则为空显示;我的代码给你参考一下:<asp:GridView ID="grdArticle" runat="server"   width="100%" DataKeyNames="Authorid" 
                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();
            }
    }
      

  4.   

    3楼的方法不适用datalist,因为datalist只显示了一个列............
      

  5.   

    sorry,刚又试了,3楼的方法确实可以,感谢感谢!!!