gridview 行和列合并

gridview rows and columns merge

我在数据表中有以下格式的数据,我试图将其绑定到 gridview (格式 -1)

 Id     col1    col2
 20      a     ac-1
 20      a     ac-2
 20      a     ac-3
 20      a     ac-4
 21      a     ac-1
 21      a     ac-2
 21      a     ac-3
 21      a     ac-4

进行数据绑定后,我得到以下格式的 gridview (format- 2)

 Id     col1    col2
 20      a      ac-1
                ac-2
                ac-3
                ac-4
 21             ac-1
                ac-2
                ac-3
                ac-4

但我正在寻找以下格式的数据以在 gridview 中表示 (Format-3)

   Id   col1    col2
   20     a     ac-1
                ac-2
                ac-3
                ac-4
   21     a     ac-1
                ac-2
                ac-3
                ac-4

以下代码用于 grdiview 中的 Ondatabound 事件

   for (int i = gvConversionGrid.Rows.Count - 1; i > 0; i--)
   {
        GridViewRow row = gvConversionGrid.Rows[i];
        GridViewRow previousRow = gvConversionGrid.Rows[i - 1];
        for (int j = 0; j < row.Cells.Count; j++)
        {
            if (row.Cells[j].Text == previousRow.Cells[j].Text)
            {
                if (previousRow.Cells[j].RowSpan == 0)
                {
                    if (row.Cells[j].RowSpan == 0)
                    {
                        previousRow.Cells[j].RowSpan += 2;
                    }
                    else
                    {
                        previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                    }
                    row.Cells[j].Visible = false;
                }
            }
        }
   }

有没有办法像 format-3..

那样操作 gridview 并为 gridview 数据带来格式

请问有人能帮忙解答一下吗,我将不胜感激.. 非常感谢

更新 : 我正在寻找以下格式的图片

您可以插入 2 个隐藏列

 Id     col1    col2  groupindex  groupcount
 20      a     1      1         4
 20      a     2      2         4
 20      a     3      3         4
 20      a     4      4         4
 21      a     1      1         5
 21      a     2      2         5
 21      a     3      3         5
 21      a     4      4         5
 21      a     5      5         5

并设置行跨度:

 void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow )
    {

        // set rowspan for the first row of group
        if (Convert.ToInt32(e.Row.Cells[3]) == 1)
        {
            var rowSpan = Convert.ToString(e.Row.Cells[4]);
            e.Row.Cells[0].Attributes.Add("rowspan", rowSpan);
            e.Row.Cells[1].Attributes.Add("rowspan", rowSpan);
        }
        else
        {
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;
        }
    }
}

您可以将先前的 Id 值设置为在方法外部声明的变量,并将当前行与 GridView 的 OnRowDataBound 事件中的行进行比较。

string previousCellValue = "";

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //check if the current id matches the previous row
        if (previousCellValue == row["Id"].ToString())
        {
            //clear the first cell
            e.Row.Cells[0].Text = "";

            //apply column span
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells[1].Visible = false;
        }
        else
        {
            previousCellValue = row["Id"].ToString();
        }
    }
}

结果:

更新

string previousCellValue = "";
int previousCellCount = 1;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow and not the first row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //check if the current id matches the previous row
        if (previousCellValue == row["Id"].ToString())
        {
            //count the number of same cells
            previousCellCount++;
        }
        else
        {
            //span the rows for the first two cells
            if (previousCellCount > 1)
            {
                GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[0].RowSpan = previousCellCount;
                GridView1.Rows[e.Row.RowIndex - previousCellCount].Cells[1].RowSpan = previousCellCount;

                //hide the other cells in the column
                for (int i = 1; i < previousCellCount; i++)
                {
                    GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[0].Visible = false;
                    GridView1.Rows[(e.Row.RowIndex - previousCellCount) + i].Cells[1].Visible = false;
                }
            }

            previousCellValue = row["Id"].ToString();
            previousCellCount = 1;
        }
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        //use the footer row to create spanning for the last rows if needed
        if (previousCellCount > 1)
        {
            GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[0].RowSpan = previousCellCount;
            GridView1.Rows[GridView1.Rows.Count - previousCellCount].Cells[1].RowSpan = previousCellCount;

            //hide the other cells in the column
            for (int i = 1; i < previousCellCount; i++)
            {
                GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[0].Visible = false;
                GridView1.Rows[(GridView1.Rows.Count - previousCellCount) + i].Cells[1].Visible = false;
            }
        }
    }
}

结果: