Вы можете сделать это в событии 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();
}
}
Пользователь просит Группировка - сетка уже отсортированы (см скриншот) –