如何仅将 Gridview 中的 Rowspan 用于第一列 [英] How to use Rowspan in Gridview for 1st Column only

查看:34
本文介绍了如何仅将 Gridview 中的 Rowspan 用于第一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

请检查我使用的代码:

 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都是空白的.

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

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

有人可以帮忙吗?

推荐答案

改用 RowDataBound 事件:

Use RowDataBound event instead:

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

这篇关于如何仅将 Gridview 中的 Rowspan 用于第一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆