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;
}
}
}
| You must Sign In to use this message board. |
|
| | Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh) | FirstPrevNext |
|
 |
|
|
 |
|
 |
Hi Thanks for the grouping datagrid . However IT seems to group all but the last row in my datagrid. I have not changed the code.
First: the last line of code seems to cut off my last row of data. Second: If I comment the last line out then the last row in the dataset is not grouped with the rest.
Any Ideas?
Thanks
modified on Monday, December 10, 2007 1:09:01 PM
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
 |
|
 |
public void GroupColumn(DataGrid dg, int ColumnIndex) { TableCell previousCell = new TableCell(); TableCell pointedCell = new TableCell(); TableCell headerCell = dg.Items[0].Cells[ColumnIndex]; int counter = 1;
foreach (DataGridItem dgi in dg.Items) {
pointedCell = dg.Items[dgi.ItemIndex].Cells[ColumnIndex];
if (pointedCell.Text != previousCell.Text) { headerCell = pointedCell; counter = 1; }
if (counter > 1) { headerCell.RowSpan = counter; pointedCell.Visible = false; } previousCell = pointedCell; counter++; } }
Marcelo
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
using System; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace GMIProject.GMIBuisinessLayer { /// /// Summary description for DataGridGrouper. /// public class DataGridGrouper { public DataGridGrouper() { // // TODO: Add constructor logic here // }
public void GroupColumn(DataGrid dg, int ColumnIndex) { int ItemIndex = 0; int Groupings = 0; if(dg.Items.Count>1) { foreach (DataGridItem dgi in dg.Items) { if (dgi.ItemIndex > 0) { //if current cells text is the same as the cell above it //make it invisible and increase the row span by 1 of the //last visible cell in that column. if (dgi.Cells[ColumnIndex].Text == dg.Items[dgi.ItemIndex-1].Cells[ColumnIndex].Text) { dgi.Cells[ColumnIndex].Visible = false; dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan = dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1; Groupings++; } else if (dg.Items[dgi.ItemIndex-1].Cells[ColumnIndex].Visible) { ItemIndex = dgi.ItemIndex; } else { dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan = dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1; ItemIndex = dgi.ItemIndex; } } } dg.Items[dg.Items.Count-2].Cells[ColumnIndex].RowSpan = dg.Items[dg.Items.Count-2].Cells[ColumnIndex].RowSpan + 1; //dg.Items[dg.Items.Count-1].Visible = false; //return Groupings; } } } }
VMSSanthosh M.sc.,MCP.,CGT., WebDeveloper Hysis Technologies Pvt Ltd Bangalore.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thank you so much..your code just saved the day for me! Thanx again! Keep up the good work!
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
 |
using System; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public class DataGridGrouper { static public void GroupColumn(DataGrid dataGrid, int columnIndex) { if (dataGrid.Items.Count <= 1) return;
string lastText = dataGrid.Items[0].Cells[columnIndex].Text; string text = null; bool groupIsEven = true; int groupStartIndex = 0;
int rowIndex = 1; for (; rowIndex < dataGrid.Items.Count; rowIndex++) { text = dataGrid.Items[rowIndex].Cells[columnIndex].Text; if (text == lastText) { dataGrid.Items[rowIndex].Cells[columnIndex].Visible = false; } else { dataGrid.Items[groupStartIndex].Cells[columnIndex].RowSpan = rowIndex - groupStartIndex; dataGrid.Items[groupStartIndex].Cells[columnIndex].CssClass = groupIsEven?"even_css_class":"odd_css_class";
groupIsEven = !groupIsEven; groupStartIndex = rowIndex; } lastText = text; } dataGrid.Items[groupStartIndex].Cells[columnIndex].RowSpan = rowIndex - groupStartIndex; dataGrid.Items[groupStartIndex].Cells[columnIndex].CssClass = groupIsEven?"even_css_class":"odd_css_class"; } } }
-- modified at 6:05 Wednesday 15th March, 2006
|
| Sign In·View Thread·PermaLink | 1.20/5 (2 votes) |
|
|
|
 |
|
 |
Thanks for the article, It´s really helpfull. 
I´d noticed that this code doesn´t render the las row of our grid
The reason is that code ... //remove the extra row - might be good to know why i have to add an
//extra row.
dgMonitor.Items[dgMonitor.Items.Count-1].Visible = false; ... As we can see It changes the last row visibility to false.
In order to solve this, I have change this code as follows. It works fine in my tests
dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan = dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 2;
Thank you¡¡
Francisco Trigo Martínez Junior Software Architect for www.proyecta.biz
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi brother What do you use as datasource for your DataGrid? Cos i use the same code as above and i use a IList as datasource ....and i still can't see 'my last row'? Do you think that this could be a matter or not? Thanks in advcance
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Yeah, that last row thing is evil--it just swallows it up. Here's what I came up with, in case it's any use to anyone.
Public Sub GroupColumn(ByVal myDataGrid As DataGrid, ByVal ColumnIndex As Integer) Dim CellPointer As Integer = 0 Dim ThisCell As TableCell Dim PreviousCell As TableCell Dim PointedCell As TableCell
For Each dgi As DataGridItem In myDataGrid.Items If dgi.ItemIndex > 0 Then ThisCell = dgi.Cells(ColumnIndex) PreviousCell = myDataGrid.Items(dgi.ItemIndex - 1).Cells(ColumnIndex) PointedCell = myDataGrid.Items(CellPointer).Cells(ColumnIndex)
If ThisCell.Text = PreviousCell.Text Then ThisCell.Visible = False PointedCell.RowSpan += 1 Else If PreviousCell.Visible = False Then PointedCell.RowSpan += 1 End If CellPointer = dgi.ItemIndex End If End If Next ' Why is this necessary? If Not (ThisCell Is Nothing OrElse PreviousCell Is Nothing OrElse PointedCell Is Nothing) Then If ThisCell.Text = PreviousCell.Text Then PointedCell.RowSpan += 1 End If End If End Sub
I'm not actually sure why that last bit is necessary, but it seems to be...
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
This is necessary because rowspan starts on '0' by default, if you increment 1 by 1 like ++ operator this is not what we really need here. We need a jump from 0 to 2
0 when you have just 1 item in the category 2 when you have 2 items 3 when you have 3 items and so on.
additionally a counter is useful to keep the items per category. When changing the category you reset the counter.
What turns the thing difficult is that if you start with 0 and go incrementing one by one you can see the problem just in the last row: one row missing to be spanned.
Marcelo
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin