/// <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;
}
}
}