【问题标题】:How to use Rowspan in Gridview for 1st Column only如何在 Gridview 中仅将 Rowspan 用于第一列
【发布时间】:2012-11-01 12:31:52
【问题描述】:

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

我尝试使用下面的代码,但对我不起作用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 都是空白的。

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

谁能帮忙?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

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

    【讨论】:

    • @Yuiry 这好多了,也很容易理解。
    【解决方案2】:
     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 我检查了上面的解决方案完美无缺,是的比我做的要好得多 :) 感谢 naveen 分享这个
    • 很乐意帮助老兄... :) 但是 yuriys 解决方案可以解决您的问题,对吗?
    【解决方案3】:
        '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
    

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 2015-10-04
      • 1970-01-01
      • 2018-07-25
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      相关资源
      最近更新 更多