Introduction
This is built off the same idea that Serge Lobko-Lobanovsky had in his article on here at http://www.codeproject.com/aspnet/GridGroupFormat.asp.
had trouble with the code he had posted and had to get it done for a project. Just call the method DataGridGroupBy(DataGrid dgYouWantGrouped, int ColumnToGroup)
of the DataGridGroupByRows
class in your WebForm and it will group all rows you want grouped together.
Here is the code for a class i wrote in the sample project attached:
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace localhost
{
public class DataGridGrouper
{
public DataGridGrouper()
{
}
public void GroupColumn(DataGrid dgMonitor, int ColumnIndex)
{
int ItemIndex= 0;
foreach(DataGridItem dgItem in dgMonitor.Items)
{
if(dgItem.ItemIndex > 0)
{
if(dgItem.Cells[ColumnIndex].Text ==
dgMonitor.Items[dgItem.ItemIndex-1].Cells[ColumnIndex].Text)
{
dgItem.Cells[ColumnIndex].Visible = false;
dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan =
dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
}
else if(dgMonitor.Items[
dgItem.ItemIndex-1].Cells[ColumnIndex].Visible == true)
{
ItemIndex = dgItem.ItemIndex;
}
else
{
dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan =
dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
ItemIndex = dgItem.ItemIndex;
}
}
}
dgMonitor.Items[dgMonitor.Items.Count-1].Visible = false;
}
}
}
Web Programmer / Analyst for Integres.com, and part owner www.golfbagstore.com