【问题标题】:Adding rows to a Gridview that is bound to a SqlDataSource将行添加到绑定到 SqlDataSource 的 Gridview
【发布时间】:2013-05-11 21:43:28
【问题描述】:

我正在尝试动态地向 gridview 添加一行,而 gridview 绑定了一个 SqlDataSource。是否可以在数据仍绑定时添加自定义行?我正在用 vb 编码。

【问题讨论】:

  • 请提供更多细节和一些示例代码。例如,是否要在 OnDataBinding 事件期间添加并在数据源中的特定行之后添加一行?还是做点别的?
  • 将数据添加到数据库并调用sqlDataSource上的Databind。

标签: asp.net vb.net gridview data-binding sqldatasource


【解决方案1】:

您不会将该行添加到网格视图中,而是将新数据添加到它的数据源,然后绑定网格视图。数据不需要在您的数据库中,您可以检索数据,然后将其添加到该对象。

Public Class MyWebForm
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim myList = generateNewList()

        myList.Add(New FooBar(4, "Four"))


        GridView1.DataSource = myList
        GridView1.DataBind()

    End Sub

    Private Function generateNewList() As List(Of FooBar)

        Dim mylist As New List(Of FooBar)

        mylist.Add(New FooBar(1, "One"))
        mylist.Add(New FooBar(2, "Two"))
        mylist.Add(New FooBar(3, "Three"))

        Return mylist

    End Function

End Class

Public Class FooBar

    Private Property myID As Integer
    Private Property myName As String

    Public Property id As Integer
        Get
            Return myID
        End Get
        Set(value As Integer)
            myID = value
        End Set
    End Property
    Public Property name As String
        Get
            Return myName
        End Get
        Set(value As String)
            myName = value
        End Set
    End Property


    Public Sub New(id As Integer, name As String)
        myID = id
        myName = name

    End Sub
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2014-09-12
    相关资源
    最近更新 更多