/// <summary>
/// 方法编号:06
/// 方法名称:CombineRepeatedCell
/// 内容摘要:文字相同的列的合并
/// 算法思路:将相同的连续单元格想象成“块”,将"块"放置在数据容器中,全部遍历相加需合并的单元格的rowspan,来进行合并隐藏的动作
/// </summary>
/// <param name="m_arrIndex">需要合并的列的index数据</param>
public void CombineRepeatedCell(DataGrid m_grid,int[] m_arrIndex)
{
foreach (int m_colIndex in m_arrIndex) //列
{
ArrayList m_hidCells = new ArrayList();
//得到需合并的item
for (int i = 0; i < m_grid.Items.Count; i++)
{
//第一行不用比较,并终止比较于最后一行
if (i == 0 || i == (m_grid.PageSize * m_grid.CurrentPageIndex))
{
m_hidCells.Add(m_grid.Items[i].Cells[m_colIndex]);
continue;
}
//文字相同时
if (string.Compare(m_grid.Items[i].Cells[m_colIndex].Text,m_grid.Items[i - 1].Cells[m_colIndex].Text) == 0)
{
m_hidCells.Add(m_grid.Items[i].Cells[m_colIndex]);
}
//文字不同,或文字相同但已到最后一行
if (m_grid.Items[i].Cells[m_colIndex].Text != m_grid.Items[i - 1].Cells[m_colIndex].Text
|| i == m_grid.Items.Count - 1)
{
//一旦发现有不同的文字,即表示单元格块的结束
if (m_hidCells.Count <= 0) //无需合并
{
return;
}
int m_iRowspan = 0; //待合并的单元格的Rowspan
foreach (TableCell c in m_hidCells)
{
m_iRowspan += (c.RowSpan > 0) ? c.RowSpan : 1;
}
for (int ii = 1; ii <= m_hidCells.Count - 1; ii++)
{
((TableCell)m_hidCells[ii]).Visible = false;
}
((TableCell)m_hidCells[0]).RowSpan = m_iRowspan;
//开始制作新的容器
m_hidCells = new ArrayList();
m_hidCells.Add(m_grid.Items[i].Cells[m_colIndex]);
continue;
}//for 结束
}//foreach 结束
}//foreach (int i in m_arrIndex) 结束
}//CombineRepeatedCell 结束
/// <summary>
/// 方法编号:03
/// 方法名称:HideRepeatedCell
/// 内容摘要:文字相同的列的隐藏
/// 算法思路:不能在循环ITEM的时候进行合并或隐藏,以免比较出现错误
/// 只能暂先放置在容器中,全部遍历方进行合并隐藏的动作
/// </summary>
/// <param name="m_grid"></param>
/// <param name="m_arrIndex">需要隐藏的列的index数据</param>
public void HideRepeatedCell(DataGrid m_grid,int[] m_arrIndex)
{
if (m_arrIndex.Length <= 0)//检查特异情况
{
return;
}
//处理重复时隐藏
ArrayList m_hidCells = new ArrayList();
//得到需隐藏的item
foreach (DataGridItem item in m_grid.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
//如果为第一行,且跳过
if (item.ItemIndex == 0 || item.ItemIndex == (m_grid.PageSize * m_grid.CurrentPageIndex))
{
continue;
}
foreach (int m_colIndex in m_arrIndex)
{
if (item.Cells[m_colIndex].Text == m_grid.Items[item.ItemIndex - 1].Cells[m_colIndex].Text)
{
m_hidCells.Add(item.Cells[m_colIndex]);
}
else
{
break;
}
}
}//if (item.ItemType == ListItemType.Item 结束
}//foreach (DataGridItem item in m_grid.Items) 结束
//开始隐藏
foreach (object obj in m_hidCells)
{
((TableCell)obj).Text = string.Empty;
}
}//HideRepeatedCell 结束
/// 方法编号:06
/// 方法名称:CombineRepeatedCell
/// 内容摘要:文字相同的列的合并
/// 算法思路:将相同的连续单元格想象成“块”,将"块"放置在数据容器中,全部遍历相加需合并的单元格的rowspan,来进行合并隐藏的动作
/// </summary>
/// <param name="m_arrIndex">需要合并的列的index数据</param>
public void CombineRepeatedCell(DataGrid m_grid,int[] m_arrIndex)
{
foreach (int m_colIndex in m_arrIndex) //列
{
ArrayList m_hidCells = new ArrayList();
//得到需合并的item
for (int i = 0; i < m_grid.Items.Count; i++)
{
//第一行不用比较,并终止比较于最后一行
if (i == 0 || i == (m_grid.PageSize * m_grid.CurrentPageIndex))
{
m_hidCells.Add(m_grid.Items[i].Cells[m_colIndex]);
continue;
}
//文字相同时
if (string.Compare(m_grid.Items[i].Cells[m_colIndex].Text,m_grid.Items[i - 1].Cells[m_colIndex].Text) == 0)
{
m_hidCells.Add(m_grid.Items[i].Cells[m_colIndex]);
}
//文字不同,或文字相同但已到最后一行
if (m_grid.Items[i].Cells[m_colIndex].Text != m_grid.Items[i - 1].Cells[m_colIndex].Text
|| i == m_grid.Items.Count - 1)
{
//一旦发现有不同的文字,即表示单元格块的结束
if (m_hidCells.Count <= 0) //无需合并
{
return;
}
int m_iRowspan = 0; //待合并的单元格的Rowspan
foreach (TableCell c in m_hidCells)
{
m_iRowspan += (c.RowSpan > 0) ? c.RowSpan : 1;
}
for (int ii = 1; ii <= m_hidCells.Count - 1; ii++)
{
((TableCell)m_hidCells[ii]).Visible = false;
}
((TableCell)m_hidCells[0]).RowSpan = m_iRowspan;
//开始制作新的容器
m_hidCells = new ArrayList();
m_hidCells.Add(m_grid.Items[i].Cells[m_colIndex]);
continue;
}//for 结束
}//foreach 结束
}//foreach (int i in m_arrIndex) 结束
}//CombineRepeatedCell 结束
/// <summary>
/// 方法编号:03
/// 方法名称:HideRepeatedCell
/// 内容摘要:文字相同的列的隐藏
/// 算法思路:不能在循环ITEM的时候进行合并或隐藏,以免比较出现错误
/// 只能暂先放置在容器中,全部遍历方进行合并隐藏的动作
/// </summary>
/// <param name="m_grid"></param>
/// <param name="m_arrIndex">需要隐藏的列的index数据</param>
public void HideRepeatedCell(DataGrid m_grid,int[] m_arrIndex)
{
if (m_arrIndex.Length <= 0)//检查特异情况
{
return;
}
//处理重复时隐藏
ArrayList m_hidCells = new ArrayList();
//得到需隐藏的item
foreach (DataGridItem item in m_grid.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
//如果为第一行,且跳过
if (item.ItemIndex == 0 || item.ItemIndex == (m_grid.PageSize * m_grid.CurrentPageIndex))
{
continue;
}
foreach (int m_colIndex in m_arrIndex)
{
if (item.Cells[m_colIndex].Text == m_grid.Items[item.ItemIndex - 1].Cells[m_colIndex].Text)
{
m_hidCells.Add(item.Cells[m_colIndex]);
}
else
{
break;
}
}
}//if (item.ItemType == ListItemType.Item 结束
}//foreach (DataGridItem item in m_grid.Items) 结束
//开始隐藏
foreach (object obj in m_hidCells)
{
((TableCell)obj).Text = string.Empty;
}
}//HideRepeatedCell 结束