/// <summary>
/// 纵向合并指定列的相同文本单元格
/// </summary>
/// <usage>
/// MergeSameCellAtTextColumn(DataGridInstance,columnIndex);
/// </usage>
/// <param name="curGrid"></param>
/// <param name="columnIndex"></param>
private void MergeSameCellAtTextColumn(DataGrid curGrid,int columnIndex)
{
DataGridItem curItem = null;
DataGridItem nextItem = null;
for (int r = 0; r < curGrid.Items.Count; r++)
{
///当前单元格
curItem = curGrid.Items[r];
///其下单元格
if(r + 1 < curGrid.Items.Count)
nextItem = curGrid.Items[r + 1];
else
nextItem = null;
string curValue = curItem == null ? string.Empty:curItem.Cells[columnIndex].Text;
string nextValue = nextItem == null ? string.Empty:nextItem.Cells[columnIndex].Text;
///如果单元格内容相同
if(curValue != string.Empty && nextValue != string.Empty && curValue == nextValue)
{
///设置其下单元格不可见
if(nextValue != null)nextItem.Cells[columnIndex].Visible = false;
///如果当前单元格可见,则将其RowSpan + 1
if(curItem.Cells[columnIndex].Visible == true)
{
curItem.Cells[columnIndex].RowSpan += 1;
}
///如果当前单元格不可见,则直接回溯到其上第一个可见单元格为止
else if(curItem.Cells[columnIndex].Visible == false)
{
int preRowIndex = r;
while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
{
preRowIndex -= 1;
}
///将其上第一个可见单元格的RowSpan + 1
curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
}
}
///如果单元格内容不同
else if(curValue != string.Empty && nextValue != string.Empty && curValue != nextValue)
{
///判断是否是临界行:上面的一行或多行与下面的一行或多行的对应单元格内容不同
if(curItem.Cells[columnIndex].Visible == false)
{
int preRowIndex = r;
while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
{
preRowIndex -= 1;
}
///将其上第一个可见单元格的RowSpan + 1
curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
}
else
{
///非临界
}
}
///数据列表的最后一行
else if(r == curGrid.Items.Count - 1 && curItem.Cells[columnIndex].Visible == false)
{
int preRowIndex = r;
while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
{
preRowIndex -= 1;
}
///将其上第一个可见单元格的RowSpan + 1
curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
}
}
}
/// <summary>
/// 纵向合并指定列的相同模板单元格
/// </summary>
/// <usage>
/// MergeSameCellAtTemplateColumn(DataGridInstance,"TemplateControlId",columnIndex);
/// </usage>
/// <param name="curGrid"></param>
/// <param name="columnIndex"></param>
/// <param name="controlName"></param>
private void MergeSameCellAtTemplateColumn(DataGrid curGrid,int columnIndex,string controlName)
{
DataGridItem curItem = null;
DataGridItem nextItem = null;
for (int r = 0; r < curGrid.Items.Count; r++)
{
///当前单元格
curItem = curGrid.Items[r];
///其下单元格
if(r + 1 < curGrid.Items.Count)
nextItem = curGrid.Items[r + 1];
else
nextItem = null;
///调用时注意修改此处的模板控件类型和模板控件获取值的方法
string curValue = curItem == null ? string.Empty:((ListBox)curItem.FindControl(controlName)).SelectedText;
string nextValue = nextItem == null ? string.Empty:((ListBox)nextItem.FindControl(controlName)).SelectedText;
///如果单元格内容相同
if(curValue != string.Empty && nextValue != string.Empty && curValue == nextValue)
{
///设置其下单元格不可见
if(nextValue != null)nextItem.Cells[columnIndex].Visible = false;
///如果当前单元格可见,则将其RowSpan + 1
if(curItem.Cells[columnIndex].Visible == true)
{
curItem.Cells[columnIndex].RowSpan += 1;
}
///如果当前单元格不可见,则直接回溯到其上第一个可见单元格为止
else if(curItem.Cells[columnIndex].Visible == false)
{
int preRowIndex = r;
while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
{
preRowIndex -= 1;
}
///将其上第一个可见单元格的RowSpan + 1
curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
}
}
///如果单元格内容不同
else if(curValue != string.Empty && nextValue != string.Empty && curValue != nextValue)
{
///判断是否是临界行:上面的一行或多行与下面的一行或多行的对应单元格内容不同
if(curItem.Cells[columnIndex].Visible == false)
{
int preRowIndex = r;
while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
{
preRowIndex -= 1;
}
///将其上第一个可见单元格的RowSpan + 1
curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
}
else
{
///非临界
}
}
///数据列表的最后一行
else if(r == curGrid.Items.Count - 1 && curItem.Cells[columnIndex].Visible == false)
{
int preRowIndex = r;
while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
{
preRowIndex -= 1;
}
///将其上第一个可见单元格的RowSpan + 1
curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
}
}
}