梦醒三分,执着疯子
没有什么不可以!
  做项目有时也会遇到要合并DataGrid单元格的情况。在DataGrid单元格中有ColumnSpan、RowSpan、Visible属性,这跟Table控件中的colspan、rowspan属性相同,因此我们可以利用这些属性来合并DataGrid单元格。以下两个合并单元格的函数,是我在项目中实际用到的。说明:prevDg为DataGrid的变量。
void ColSpan()//合并列单元格
  {
   for(int i=0;i<prevDg.Items.Count;i++)
   {
    if (prevDg.Items[i].Cells[3].Text.Trim()=="0" && prevDg.Items[i].Cells[4].Text.Trim()=="0" )
    {
     prevDg.Items[i].Cells[2].ColumnSpan=3;
     prevDg.Items[i].Cells[3].Visible=false;
     prevDg.Items[i].Cells[4].Visible=false;
    }
   }
  }
  void RowSpan()//合并行单元格
  {
   int j;
   int n;
   for(int i=0;i<prevDg.Items.Count;i++)
   {
    n=1;
    for (j=i+1;j<prevDg.Items.Count;j++)
    {
     if(prevDg.Items[i].Cells[0].Text.Trim()==prevDg.Items[j].Cells[0].Text.Trim() && prevDg.Items[i].Cells[7].Text.Trim()==prevDg.Items[j].Cells[7].Text.Trim())
     {
      n += 1;
      prevDg.Items[i].Cells[0].RowSpan=n;
      prevDg.Items[j].Cells[0].Visible=false;
     }
     else break;
    }
    i=j-1;
   }               
  }
posted on 2006-09-26 09:11  Jacker.W  阅读(3608)  评论(6编辑  收藏  举报