C# 如何在 Gridview 中仅将 Rowspan 用于第一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13177139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:42:33  来源:igfitidea点击:

How to use Rowspan in Gridview for 1st Column only

c#asp.netgridview

提问by Pratik

Need help to resolve a issue related with Gridview layout. I am trying to implement custome Gridview with Itemtemplate Columns using C#.Net language and want to include view using RowSpan property.

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

As Below

如下

I tried to use below code but didn't work for me Here

我尝试使用下面的代码,但对我不起作用这里

Please check the code which i used:

请检查我使用的代码:

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

}

But every time the gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Textis blank.

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

Hence the grid is taking weird shapes. Don't know what is happening here.

因此,网格呈现出奇怪的形状。不知道这里发生了什么。

Can anyone help?

任何人都可以帮忙吗?

采纳答案by Yuriy Rozhovetskiy

Use RowDataBound event instead:

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

回答by Pratik

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

This worked our for me. Trial & error :)

这对我有用。试错:)

回答by KhaledDev

    '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