2017-01-10 7 views
1

мне нужна группировка в отсортированной осине: Gridview, как следующая картина показывает:Группировке в отсортированной осине: GridView

Example for Grouping

Может кто-нибудь дать мне подсказку о том, как действовать дальше? Можно ли каким-то образом группировать RowBound?

+0

Пользователь просит Группировка - сетка уже отсортированы (см скриншот) –

ответ

2

Вы можете использовать следующий алгоритм, чтобы сделать вашу работу:

  /**************************************** 
      * Grouping Algorithmus 
      * by Björn Karpenstein 
      * http://www.capri-soft.de/blog 
      ****************************************/ 
      // Nach dieser Spalte soll gruppiert werden 
      int k = 1; 

      // Für alle Zeilen (VON UNTEN NACH OBEN) 
      for (int i = GridView1.Rows.Count - 1; i > 0; i--) 
      { 
       GridViewRow row = GridView1.Rows[i]; 
       GridViewRow previousRow = GridView1.Rows[i - 1]; 
       // Für alle Spalten 
       for (int j = 0; j < row.Cells.Count; j++) 
       { 
        if ((row.Cells[k].Text == previousRow.Cells[k].Text) && (row.Cells[j].Text == previousRow.Cells[j].Text)) 
        { 
         if (previousRow.Cells[j].RowSpan == 0) 
         { 
          if (row.Cells[j].RowSpan == 0) 
          { 
           previousRow.Cells[j].RowSpan += 2; 
          } 
          else 
          { 
           previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1; 
          } 
          row.Cells[j].Visible = false; 
         } 
        } 
       } 
      } 
1

Вы можете сделать это в событии RowDataBound. Вы должны помнить, что rowspan, и удаление следующей ячейки должно быть сделано назад, так как следующая строка еще не существует. row["myValue"] - это столбец, содержащий значения, которые необходимо сравнить.

string groupValue = string.Empty; 
int rowSpanCount = 1; 

//change this to the column index that needs spanning 
int columnIndex = 6; 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the current row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //get the current row number 
     int currentRow = e.Row.DataItemIndex; 

     //cast the current row to a datarowview 
     DataRowView row = e.Row.DataItem as DataRowView; 

     //check if the groupValue equals the current row, if so +1 rowspan in needed 
     if (groupValue == row["myValue"].ToString()) 
     { 
      rowSpanCount++; 
     } 
     else if (currentRow > 1 && rowSpanCount > 1) 
     { 
      //apply rowspan to the first cell 
      GridView1.Rows[currentRow - rowSpanCount].Cells[columnIndex].RowSpan = rowSpanCount; 

      //remove the spanned rows 
      for (int i = 1; i < rowSpanCount; i++) 
      { 
       GridView1.Rows[currentRow - (rowSpanCount - i)].Cells.RemoveAt(columnIndex); 
      } 

      //reset the rowSpanCount 
      rowSpanCount = 1; 
     } 

     //set the groupValue variable for value comparison in the next row 
     groupValue = row["myValue"].ToString(); 
    } 
}