DataGrid相邻行有相同内容时对指定列合并

/// <summary>
/// DataGrid相邻行有相同内容时对指定列合并
/// </summary>
/// <param name="spangrid">格式化的DataGrid的ID</param>
/// <param name="spancell">要合并的列</param>        
/// <param name="spanby">合并所依据数据的列</param>

    public void FormatGrid(DataGrid spangrid,int spancell,int spanby)
    
{
      
if(spanby<0 || spanby>spangrid.Items.Count)
          
return;
          
int rowspan = 1;
          
for(int i = 1;i<spangrid.Items.Count;i++)
         
{
        
if(spangrid.Items[i].Cells[spanby].Text == spangrid.Items[i-1].Cells[spanby].Text)
            
{
                
               rowspan 
+=1;
               spangrid.Items[i].Cells[spancell].Visible 
= false;
               spangrid.Items[i
-rowspan+1].Cells[spancell].RowSpan = rowspan;
            }

        
else
        
{    
           
string str = spangrid.Items[i].Cells[spanby].Text;
           
string str1 = spangrid.Items[i-1].Cells[spanby].Text;
           rowspan 
= 1;
        }
    
              }

    }
 
posted on 2005-12-03 14:09  唐朝  阅读(322)  评论(0编辑  收藏  举报