【问题标题】:Add headers VB.net/asp.net gridview?添加标题VB.net/asp.net gridview?
【发布时间】:2011-05-22 06:57:51
【问题描述】:

我知道这个问题在 C# 中得到了回答,我一直在尝试转换并让它工作,但没有成功?

如果您能提供帮助,我将不胜感激

这是来自那个问题的图片,我想做类似的事情。

这是链接 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)

【问题讨论】:

标签: asp.net vb.net gridview


【解决方案1】:

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

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

http://converter.telerik.com/

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

【讨论】:

  • 第一个也是我最喜欢的大型转换转换器 :)
【解决方案2】:

最简单的方法是在 RowCreated 事件上进行处理。您将测试正在处理的 RowType,如果它是标题,那么您:

  1. 清除有问题的行单元格。
  2. 将第一个单元格的 Rowspan 调整为 2(对于您的图像:已发货数量)。
  3. 删除紧随其后的行单元格(对于您的图像:Total Sales)。
  4. 创建一个新表,使其看起来像您想要的那样。
  5. 将新表格插入所需的行单元格中。

如果您已经有了想要使用的解决方案,但它是 C# 语言并且您无法转换它,请试试这个工具:http://www.developerfusion.com/tools/convert/csharp-to-vb/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2019-12-25
    • 2012-10-24
    相关资源
    最近更新 更多