我想通过编辑我的 SqlDataSource 根据从 DropDownList 中选择的值来更新 GridView

问题描述 投票:0回答:1

我试图通过更新 SqlDataSource 上的 SelectCommand、清除 GridView,然后根据下拉列表中的值重新应用 GridView.DataBind() 来实现此目的。它不起作用,老实说,我对此完全陌生,所以我很可能只是做错了事情。这是一些示例代码,可以让您了解我所得到的内容:

Protected Sub drp_product_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drp_product.SelectedIndexChanged

    grd_ingredientview.DataSource = Nothing
    grd_ingredientview.DataBind()
    SqlDataSource1.SelectCommand = "blah blah sample select command" + drp_product.SelectedValue
    grd_ingredientview.DataSource = SqlDataSource1
    grd_ingredientview.DataBind()

End Sub

再次,这不起作用,并抛出一个错误,说:

Both DataSource and DataSourceID are defined on 'grd_ingredientview'. Remove one definition.

任何与此相关的帮助,以及为什么这不起作用以及您的建议如何起作用的描述,我们将不胜感激。这个还是要学习很多东西的。谢谢!

asp.net vb.net gridview data-binding sqldatasource
1个回答
1
投票

好吧,首先使用代码加载网格视图,然后您可以转储 datasoruce1. (只需将其从网络表单中删除即可)。 (但如果出现提示,请回答“否”以重新创建字段并更新网格视图)。

所以,是的,使用向导,让它创建该数据源。选择您的列。测试网格。好的,现在可以了。现在网格已经建成了吗?

然后从页面中删除Datasource1。然后对于属性表,删除数据源设置。这个:

因此,现在我们从页面中删除了 Datasource1 对象以及指向该数据源的设置。

对组合框执行相同的操作。使用向导,甚至让它为组合框构建+添加数据源。再次删除数据源,然后再次删除组合框的数据源设置,就像我们对网格所做的那样。

所以,现在我们可以(并且将会)自由地使用代码来填写数据网格。

因此,在页面加载时,一次性(第一次)数据加载,我们可以这样做:

    If IsPostBack = False Then
        ' first time page load - display the grid.
        Call LoadGrid("")
        Call LoadCombobox
    End If

请注意,对于组合框?同样,您可以自由地使用向导来设置组合框,但再次删除该数据源设置,就像我们对网格所做的那样。

所以,现在我们所有的代码如下所示:

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

    If IsPostBack = False Then
        ' first time page load - display the grid.
        Call LoadGrid("")
        Call LoadCombobox
    End If

End Sub

Sub LoadGrid(Optional strWhere As String = "")

    Dim strSQL As String = "SELECT * from tblHotels "
    If strWhere <> "" Then
        strSQL += " WHERE " & strWhere
    End If

    Me.GridView1.DataSource = Myrst(strSQL)
    Me.GridView1.DataBind()

End Sub

Sub LoadComboBox()
    Dim strSQL As String = "SELECT City from tblHotels GROUP BY City"

    Me.DropDownList1.DataSource = Myrst(strSQL)
    Me.DropDownList1.DataBind()

End Sub

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged

    Dim strWhere As String
    strWhere = "City = '" & DropDownList1.SelectedValue & "'"
    Call LoadGrid(strWhere)

End Sub

现在我唯一遗漏的部分是 MyRst()。它只是返回一个数据表。因此,您可以使用该代码(并将其放置在标准代码模块的外部。

这样你就可以在你的应用程序中一遍又一遍地使用它,而不必一遍又一遍地创建连接对象和 sql 命令,直到你发疯为止。

这也意味着您只能在代码中的一个位置使用/获取/抓取连接字符串,因此当您更改数据库时很容易更改它。

因此,方便的花花公子例程可以让您不必一遍又一遍地重写查询代码,连接对象是这样的:

Public Function Myrst(strSQL As String, Optional strCon As String = "") As DataTable

    ' this also allows one to pass custom connection string - 
    ' if not passed, then default

    If strCon = "" Then
        strCon = My.Settings.TestDatabase3
    End If

    Using mycon As New SqlConnection(strCon)

      Dim oReader As New SqlDataAdapter
      Dim rstData As New DataSet

      oReader.SelectCommand = New SqlCommand(strSQL, mycon)

      Try
        oReader.Fill(rstData)
        Return rstData.Tables(0)
      Catch
        Return Nothing
      End Try
  End Using

End Function

所以,有了上面简单的辅助例程,那么代码就很容易写了。事实上,我发现它的代码甚至比桌面代码甚至在 MS-Access 中编写代码还要少。

因此上面的简单示例将从组合框中选择一个城市,然后过滤/设置网格以仅显示该城市。 如果需要,您不必在页面加载时填充网格,因为用户即将选择组合框值。因此,只需在页面加载时注释掉 LoadGrid,并且仅在进行组合(下拉列表)选择时调用它。

如上所述,不要忘记设置下拉列表自动回发 = true。

总而言之?上面的代码不多,不再是说在桌面上执行此操作,甚至在 Access 中执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.