如何在gridview中合并单元格日期时间

时间:2017-05-09 01:53:34

标签: c# asp.net

我在gridview中有一列显示日期和时间。但我想基于相同的日期合并列,虽然时间不一样。怎么做到这一点?此代码和输出我尝试到目前为止,只合并其他列而不是列datetime。

  public void GridView_Row_Merger(GridView gridView)
{
    for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow currentRow = gridView.Rows[rowIndex];
        GridViewRow previousRow = gridView.Rows[rowIndex + 1];

        for (int i = 0; i < currentRow.Cells.Count; i++)
        {
            if (currentRow.Cells[i].Text == previousRow.Cells[i].Text)
            {
                if (previousRow.Cells[i].RowSpan < 2)
                    currentRow.Cells[i].RowSpan = 2;
                else
                    currentRow.Cells[i].RowSpan = previousRow.Cells[i].RowSpan + 1;
                previousRow.Cells[i].Visible = false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚得到答案!这是代码。谢谢你们的帮助!

 public void GridView_Row_Merger(GridView gridView)
{
    for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow currentRow = gridView.Rows[rowIndex];
        GridViewRow previousRow = gridView.Rows[rowIndex + 1];
        string[] arra1 = currentRow.Cells[0].Text.Split(' ');
        string[] arra2 = previousRow.Cells[0].Text.Split(' ');
        currentRow.Cells[0].Text = arra1[0];
        previousRow.Cells[0].Text = arra2[0];
        if (currentRow.Cells[0].Text == previousRow.Cells[0].Text)
        {
            if (previousRow.Cells[0].RowSpan < 2)
            {
                currentRow.Cells[0].RowSpan = 2;
            }
            else
            {
                currentRow.Cells[0].RowSpan = previousRow.Cells[0].RowSpan + 1;
            }
            previousRow.Cells[0].Visible = false;
        }
    }
}