如何在GridView中仅对第一列使用Rowspan

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

As Below

我尝试使用以下代码,但对我无效 这里 请检查我使用的代码:
 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;
            }
        }
    }

}

但每次gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text为空。
因此,网格会呈现奇怪的形状。不知道这里发生了什么。
有人能帮忙吗?
3个回答

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

3
 protected void GridView31_DataBound1(object sender, EventArgs e)
{
    int i = 1;
    for (int rowIndex = grdView31.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow gvRow = grdView31.Rows[rowIndex];
        GridViewRow gvPreviousRow = grdView31.Rows[rowIndex + 1];

        if (i % 4 !=0)
        {
            if (gvPreviousRow.Cells[0].RowSpan < 2)
            {
                gvRow.Cells[0].RowSpan = 2;
            }
            else
            {
                gvRow.Cells[0].RowSpan = gvPreviousRow.Cells[0].RowSpan + 1;
            }
            gvPreviousRow.Cells[0].Visible = false;
        }
        i++;
    }

这对我有效。尝试和错误 :)

@naveen 我已经检查了上面的解决方案,它完美地运行了,比我做的好多了 :) 感谢你分享这个。 - Pratik
很高兴能帮忙,但是Yuriy的解决方案已经解决了你的问题,对吧? - naveen

0
    'VB.NET Code


Private Sub DG_Data_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles DG_Data.RowDataBound
            Try

                'fusion , rowspan , colspan , 
                If e.Row.RowType = DataControlRowType.DataRow Then
                    If e.Row.RowIndex Mod 4 = 0 Then
                        e.Row.Cells(0).Attributes.Add("rowspan", "4")
                    Else
                        e.Row.Cells(0).Visible = False
                    End If
                End If



            Catch ex As Exception
                jq.msgErrorLog(ex)
            End Try
        End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接