【问题标题】:ASP.NET GridView is not dynamically filledASP.NET GridView 不是动态填充的
【发布时间】:2020-09-10 22:03:53
【问题描述】:

我正在尝试在 ASP.NET 中创建一个动态绑定的 GridView。
我已经在 aspx 文件中定义的 GridView 本身。但是我需要动态创建的所有列和行。 SQL DataBind 对我来说不是一个解决方案,因为我最终需要执行额外的逻辑。

代码如下:

<asp:GridView ID="GridView1" runat="server">

</asp:GridView> 

代码隐藏:

Protected Sub CreateGrid()
    Dim table As DataTable = New DataTable
    table.Columns.Add(New DataColumn("Name", GetType(BoundField)))

    Dim i As Integer = 0
    While i < 10
        Dim dr As DataRow = table.NewRow()
        dr("Name") = i.ToString()
        table.AcceptChanges()

        i += 1
    End While

    GridView1.DataSource = table
    GridView1.DataBind()
End Sub

【问题讨论】:

    标签: asp.net vb.net gridview webforms


    【解决方案1】:

    您正在创建行但从未将它们添加到表中:

    Protected Sub CreateGrid()
        Dim table As New DataTable
    
        table.Columns.Add("Name", GetType(String))
    
        For i = 0 To 9
            Dim dr As DataRow = table.NewRow()
    
            dr("Name") = i.ToString()
            table.Rows.Add(dr)
        Next
    
        table.AcceptChanges()
    
        GridView1.DataSource = table
        GridView1.DataBind()
    End Sub
    

    我免费进行了其他一些改进。 AcceptChanges 调用也可能没有充分的理由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-26
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2021-10-29
      相关资源
      最近更新 更多