2013-01-09 91 views
0

我在Gridview中遇到了一個問題,如果Gridview沒有記錄,我不能合併報頭,但如果Gridview包含記錄,報頭正在合併。在Gridview中合併報頭

這裏是我的代碼,我用在GridView

protected void grdWorkExperience_RowCreated(object sender, GridViewRowEventArgs e) 
    { 

     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      GridView oGridView = (GridView)sender; 
      GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); 
      TableCell oTableCell = new TableCell(); 

      oTableCell.Text = string.Empty; 
      oTableCell.ColumnSpan = 2; 
      oTableCell.BorderColor = System.Drawing.Color.White; 
      oGridViewRow.Cells.Add(oTableCell); 

      oTableCell = new TableCell(); 
      oTableCell.Text = "Inclusive Dates(mm/dd/yyyy)"; 
      oTableCell.ColumnSpan = 2; 
      oTableCell.Font.Bold = true; 
      oTableCell.Font.Size = 9; 
      oTableCell.Font.Name = "Verdana"; 
      oTableCell.HorizontalAlign = HorizontalAlign.Center; 
      oTableCell.BackColor = System.Drawing.Color.FromArgb(0x33, 0x66, 0xCC); 
      oTableCell.ForeColor = System.Drawing.Color.White; 
      oTableCell.BorderColor = System.Drawing.Color.Gray; 
      oGridViewRow.Cells.Add(oTableCell); 

      oTableCell = new TableCell(); 
      oTableCell.BorderColor = System.Drawing.Color.White; 
      oTableCell.ColumnSpan = 13; 
      oGridViewRow.Cells.Add(oTableCell); 

      oGridView.Controls[0].Controls.AddAt(0, oGridViewRow); 

     } 
    } 

合併標題我放在該功能onRowCreated事件的GridView

我應該怎麼做合併頭即使Gridview沒有記錄?

回答

0

OnRowCreated事件移動代碼來Load事件的Window/Control/Page

+0

我嘗試過,但它不工作我更改此代碼 「oGridView.Controls [0] .Controls.AddAt(0,oGridViewRow);」到這個「grdWorkExperience.Controls [0] .Controls.AddAt(0,oGridViewRow);」 我將oGridView更改爲grdWorkExperience,它是gridview的名稱,我更改的代碼行正在產生這種錯誤:「指定的參數超出了有效值範圍。 參數名稱:index」 –

+0

gridview是如果我將代碼放入頁面的加載事件中,則在我綁定gridview後發生故障。 –

+0

還有沒有其他的方法@Tilak –