添加标题VB.net/asp.net gridview?

时间:2010-12-08 14:54:30

标签: asp.net vb.net gridview

我知道这个问题已在C#中得到解答,而且我一直试图转换并使其工作,但是还没有成功?

如果你帮助了我会非常感激

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)

2 个答案:

答案 0 :(得分:3)

从您的问题来看,您似乎无法将c#转换为vb.net。这里有2个可以提供帮助的在线转换器。

http://www.developerfusion.com/tools/convert/csharp-to-vb/

http://converter.telerik.com/

或者tangible software有一个可以转换整个项目的转换器,但请注意它不是免费的。

答案 1 :(得分:0)

最简单的方法是在RowCreated事件上处理此问题。您将测试正在处理的RowType,如果是标题,则:

  1. 清除相关的行单元格。
  2. 将第一个单元格的Rowspan调整为2(如果是您的图片:已发货数量)。
  3. 立即删除行单元格(如果是您的图片:总销售额)。
  4. 创建一个新表,看起来就像您想要的那样。
  5. 将新表格插入所需的行单元格。
  6. 如果您已经拥有了希望使用的解决方案,但它已在C#中而您无法转换,请尝试使用此工具:http://www.developerfusion.com/tools/convert/csharp-to-vb/