首页 新闻 赞助 找找看

下面是自定义gridview的代码,实现的效果是合并相同行的效果

0
[待解决问题]

namespace CommonClassLibrary
{
   
publicclass GroupedGridView : GridView
    {  
       
//设置合并的列数
        publicint GroupedDepth
        {
           
get
            {
               
object val =this.ViewState["GroupedDepth"];
               
if (null== val)
                {
                   
return0;
                }

               
return (int)val;
            }
           
set
            {
               
if (value <0)
                   
thrownew ArgumentOutOfRangeException("value", "value must be greater than or equal to zero");

               
this.ViewState["GroupedDepth"] = value;
            }
        }

       
protectedoverridevoid OnDataBound(EventArgs e)
        {
           
base.OnDataBound(e);

           
this.SpanCellsRecursive(0, 0, this.Rows.Count);
        }

       
privatevoid SpanCellsRecursive(int columnIndex, int startRowIndex, int endRowIndex)
        {
           
if (columnIndex >=this.GroupedDepth || columnIndex >=this.Columns.Count)
               
return;

            TableCell groupStartCell
=null;
           
int groupStartRowIndex = startRowIndex;

           
for (int i = startRowIndex; i < endRowIndex; i++)
            {
                TableCell currentCell
=this.Rows[i].Cells[columnIndex];

               
bool isNewGroup = (null== groupStartCell) || (0!= String.CompareOrdinal(currentCell.Text, groupStartCell.Text));

               
if (isNewGroup)
                {
                   
if (null!= groupStartCell)
                    {
                        SpanCellsRecursive(columnIndex
+1, groupStartRowIndex, i);
                    }

                    groupStartCell
= currentCell;
                    groupStartCell.RowSpan
=1;
                    groupStartRowIndex
= i;
                }
               
else
                {
                    currentCell.Visible
=false;
                    groupStartCell.RowSpan
+=1;
                }
            }

            SpanCellsRecursive(columnIndex
+1, groupStartRowIndex, endRowIndex);
        }
    }
}


一缕秋丝的主页 一缕秋丝 | 初学一级 | 园豆:3
提问于:2011-11-05 19:31
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册