2012-11-01 9 views
6

Cần trợ giúp để giải quyết vấn đề liên quan đến bố cục GridView. Tôi đang cố gắng thực hiện custome GridView với Column Itemtemplate bằng cách sử dụng ngôn ngữ C#. Net và muốn bao gồm xem bằng cách sử dụng tài sản RowSpan.Cách sử dụng Rowspan trong Gridview cho Cột 1 chỉ

As Below

tôi đã cố gắng sử dụng bên dưới mã nhưng không làm việc cho tôi Here

Vui lòng kiểm tra mã mà tôi sử dụng:

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

} 

Nhưng mỗi khi gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text là trống.

Do đó lưới đang lấy hình dạng lạ. Không biết chuyện gì đang xảy ra ở đây.

Có ai giúp được không?

Trả lời

11

Sử dụng RowDataBound sự kiện thay vì:

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 Điều này tốt hơn và rất dễ hiểu. – 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++; 
    } 

Điều này làm việc cho tôi. Trial & lỗi :)

+1

tốt hơn nhiều một ... http://marss.co.ua/MergingCellsInGridView.aspx – naveen

+0

@naveen tôi đã kiểm tra các giải pháp trên hoạt động hoàn hảo & có tốt hơn nhiều so với những gì tôi đã làm :) Cảm ơn naveen đã chia sẻ này – Pratik

+0

hạnh phúc để giúp dude ... :) nhưng giải pháp yuriys giải quyết quyền prob của bạn? – 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