2010-12-08 85 views
2

我知道這個問題在C#中得到了回答,我一直在嘗試轉換並使其工作,但尚未成功?添加標題VB.net/asp.net gridview?

,如果你幫

image

這是這個問題的圖像我將不勝感激,我想要做類似的事情。

這裏是鏈接 ASP.NET GridView second header row to span main header row

Dim d As Date = Date.Today 
     d = d.AddDays(-1) 
     Label1.Text = d 

     'connects to datawarehouse 
     saocmd1.Connection = conn1 
     conn1.Open() 

     Dim ds As New DataSet 

     'selects sql query 
     'saocmd1.CommandText = MYQUERY" 
     saoda1.Fill(saods1, "salesasoftable") 

     Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal) 

     Dim left As TableCell = New TableHeaderCell() 
     left.ColumnSpan = 3 
     row.Cells.Add(left) 

     Dim totals As TableCell = New TableHeaderCell() 
     totals.ColumnSpan = gridview1.Columns.Count - 3 
     totals.Text = "Totals" 
     row.Cells.Add(totals) 

我的錯誤

Specified argument was out of the range of valid values. 
Parameter name: index 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. 
Parameter name: index 

Source Error: 


Line 54:  row.Cells.Add(totals) 
Line 55: 
Line 56:  Dim t As Table = TryCast(gridview1.Controls(0), Table) 
Line 57:  If t IsNot Nothing Then 
Line 58:   t.Rows.AddAt(0, row) 

答案

Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal) 

'spanned cell that will span the columns I don't want to give the additional header 
Dim left As TableCell = New TableHeaderCell() 
left.ColumnSpan = 6 
row.Cells.Add(left) 

'spanned cell that will span the columns i want to give the additional header 
Dim totals As TableCell = New TableHeaderCell() 
totals.ColumnSpan = myGridView.Columns.Count - 3 
totals.Text = "Additional Header" 
row.Cells.Add(totals) 

'Add the new row to the gridview as the master header row 
'A table is the only Control (index[0]) in a GridView 
DirectCast(myGridView.Controls(0), Table).Rows.AddAt(0, row) 
+0

什麼問題?提供問題的鏈接。 – 2010-12-08 14:56:57

+0

@geoff我會忘記添加鏈接。 – MyHeadHurts 2010-12-08 14:58:03

回答

0

最簡單的方法是在RowCreated事件上處理這個問題。您將測試正在處理的RowType,並且如果它是標題,則您:

  1. 清除有問題的行單元格。
  2. 將第一個單元格的行距調整爲2(如果是圖片,則爲數量發送)。
  3. 立即刪除行單元格(在您的圖像的情況下:總銷售額)。
  4. 創建一個如表所示的新表格。
  5. 將您的新表插入所需的行單元格中。

如果你已經擁有你希望使用的解決方案,但它在C#中,你不能將它,試試這個工具:http://www.developerfusion.com/tools/convert/csharp-to-vb/