2012-11-01 20 views
6

需要幫助來解決與Gridview佈局相關的問題。我正在嘗試使用C#.Net語言實現custome Gridview和Itemtemplate Columns,並希望使用RowSpan屬性包含視圖。如何在Gridview中僅使用第一列的Rowspan

As Below

我嘗試使用下面的代碼,但沒有工作對我來說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是空白時間。

因此,網格採取奇怪的形狀。不知道這裏發生了什麼。

任何人都可以幫忙嗎?

回答

11

使用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; 
     } 
    } 
} 
+0

@Yuiry這更好,也更容易理解。 – Pratik

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

這對我有效。試用&錯誤:)

+1

更好的一個... http://marss.co.ua/MergingCellsInGridView.aspx – naveen

+0

@naveen我檢查了上述解決方案完美的作品和是比我做得更好:)感謝naveen分享這 – Pratik

+0

樂意幫助兄弟...... :)但yuriys解決方案解決您的概率權利? – 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