Asp.Net 中實現DataGrid指定列的彙總功能,支援模板列彙總

來源:互聯網
上載者:User

  /// <summary>
  /// 縱向合并指定列的相同文本儲存格  
  /// </summary>
  /// <usage>
  /// MergeSameCellAtTextColumn(DataGridInstance,columnIndex);
  /// </usage>
  /// <param name="curGrid"></param>
  /// <param name="columnIndex"></param>
  private void MergeSameCellAtTextColumn(DataGrid curGrid,int columnIndex)
  {
   DataGridItem  curItem = null;
   DataGridItem  nextItem = null;
   for (int r = 0; r < curGrid.Items.Count; r++)
   {
    ///目前的儲存格
    curItem = curGrid.Items[r];
    ///其下儲存格
    if(r + 1 < curGrid.Items.Count)
     nextItem = curGrid.Items[r + 1];
    else
     nextItem = null;
    string curValue = curItem == null ? string.Empty:curItem.Cells[columnIndex].Text;
    string nextValue = nextItem == null ? string.Empty:nextItem.Cells[columnIndex].Text;
    ///如果儲存格內容相同
    if(curValue != string.Empty && nextValue != string.Empty && curValue == nextValue)
    {
     
     ///設定其下儲存格不可見
     if(nextValue != null)nextItem.Cells[columnIndex].Visible = false;
     ///如果目前的儲存格可見,則將其RowSpan + 1
     if(curItem.Cells[columnIndex].Visible == true)
     {
      curItem.Cells[columnIndex].RowSpan += 1;      
     }
      ///如果目前的儲存格不可見,則直接回溯到其上第一個可見儲存格為止
     else if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///將其上第一個可見儲存格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }
    } 
    ///如果儲存格內容不同
    else if(curValue != string.Empty && nextValue != string.Empty && curValue != nextValue)
    {
     ///判斷是否是臨界行:上面的一行或多行與下面的一行或多行的對應儲存格內容不同
     if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///將其上第一個可見儲存格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }     
     else
     {
      ///非臨界
     }
    }
    ///資料列表的最後一行
    else if(r == curGrid.Items.Count - 1 && curItem.Cells[columnIndex].Visible == false)
    {
     int preRowIndex = r;
     while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
     {
      preRowIndex -= 1;
     }
     ///將其上第一個可見儲存格的RowSpan + 1
     curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
    }
   }
   
  } 
  /// <summary>
  /// 縱向合并指定列的相同模板儲存格
  /// </summary>
  /// <usage>
  /// MergeSameCellAtTemplateColumn(DataGridInstance,"TemplateControlId",columnIndex);
  /// </usage>
  /// <param name="curGrid"></param>
  /// <param name="columnIndex"></param>
  /// <param name="controlName"></param>
  private void MergeSameCellAtTemplateColumn(DataGrid curGrid,int columnIndex,string controlName)
  {
   DataGridItem  curItem = null;
   DataGridItem  nextItem = null;
   for (int r = 0; r < curGrid.Items.Count; r++)
   {
    ///目前的儲存格
    curItem = curGrid.Items[r];
    ///其下儲存格
    if(r + 1 < curGrid.Items.Count)
     nextItem = curGrid.Items[r + 1];
    else
     nextItem = null;
    ///調用時注意修改此處的模板控制項類型和模板控制項擷取值的方法
    string curValue = curItem == null ? string.Empty:((ListBox)curItem.FindControl(controlName)).SelectedText;
    string nextValue = nextItem == null ? string.Empty:((ListBox)nextItem.FindControl(controlName)).SelectedText;
    ///如果儲存格內容相同
    if(curValue != string.Empty && nextValue != string.Empty && curValue == nextValue)
    {
     ///設定其下儲存格不可見
     if(nextValue != null)nextItem.Cells[columnIndex].Visible = false;
     ///如果目前的儲存格可見,則將其RowSpan + 1
     if(curItem.Cells[columnIndex].Visible == true)
     {
      curItem.Cells[columnIndex].RowSpan += 1;      
     }
      ///如果目前的儲存格不可見,則直接回溯到其上第一個可見儲存格為止
     else if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///將其上第一個可見儲存格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }
    } 
    ///如果儲存格內容不同
    else if(curValue != string.Empty && nextValue != string.Empty && curValue != nextValue)
    {
     ///判斷是否是臨界行:上面的一行或多行與下面的一行或多行的對應儲存格內容不同
     if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///將其上第一個可見儲存格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }
     else
     {
      ///非臨界
     }
    }
    ///資料列表的最後一行
    else if(r == curGrid.Items.Count - 1 && curItem.Cells[columnIndex].Visible == false)
    {
     int preRowIndex = r;
     while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
     {
      preRowIndex -= 1;
     }
     ///將其上第一個可見儲存格的RowSpan + 1
     curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
    }
   }
   
  } 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.