 |
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
Hi,
The mentioned sample does not work if the first column of the datagrid is a checkbox
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
Thank you so much..your code just saved the day for me! Thanx again! Keep up the good work!
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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...
|
|
|
|
 |
|
 |
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
|
|
|
|
 |