如何仅在第一列的Gridview中使用Rowspan

Pra*_*tik 9 c# asp.net gridview

需要帮助来解决与Gridview布局相关的问题.我正在尝试使用C#.Net语言实现具有Itemtemplate列的自定义Gridview,并希望使用RowSpan属性包含视图.

如下

我尝试使用下面的代码,但在我这里没有用

请检查我使用的代码:

 protected void GridView31_DataBound1(object sender, EventArgs e)
{
    for (int rowIndex = grdView31.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow gvRow = grdView31.Rows[rowIndex];
        GridViewRow gvPreviousRow = grdView31.Rows[rowIndex + 1];
        for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
        {
            if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
            {
                if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                {
                    gvRow.Cells[cellCount].RowSpan = 2;
                }
                else
                {
                    gvRow.Cells[cellCount].RowSpan =
                        gvPreviousRow.Cells[cellCount].RowSpan + 1;
                }
                gvPreviousRow.Cells[cellCount].Visible = false;
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

但每次gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text都是空白.

因此,网格采用奇怪的形状.不知道这里发生了什么.

有人可以帮忙吗?

Yur*_*kiy 13

改为使用RowDataBound事件:

void GridView31_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow )
    {
        if (e.Row.RowIndex % 4 == 0)
        {
            e.Row.Cells[0].Attributes.Add("rowspan", "4");
        }
        else
        {
            e.Row.Cells[0].Visible = false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)