问题描述
我正在尝试通过更新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.
任何对此的帮助,以及有关为什么不起作用以及您的建议如何起作用的描述,都将受到赞赏。仍然学习很多。谢谢!
解决方法
首先,使用代码加载网格视图,然后可以转储datasoruce1。 (只需将其从网络表单中删除)。 (但提示时,请回答NO,以重新创建字段并更新网格视图。)
是的,请使用向导,让它创建该数据源。选择您的列。测试网格。好的,现在可以了。现在已经建立了网格?
然后从页面中删除Datasourc1。然后对于属性表,删除数据源设置。这个:
因此,现在我们从页面中删除了该Datasource1对象,并删除了指向该数据源的设置。
并使用组合框执行相同的想法。使用向导,甚至让它为组合框构建+添加数据源。再次,删除数据源,然后再次删除组合框的数据源设置,就像我们对网格所做的一样。
因此,现在我们可以(并且将)自由地使用代码来填充数据网格。
因此,在页面加载中,一次(第一次)数据加载,我们可以这样做:
If IsPostBack = False Then
' first time page load - display the grid.
Call LoadGrid("")
Call LoadCombobox
End If
请注意,对于组合框?同样,您可以自由使用向导来设置组合框,但是AGAIN会像我们为网格所做的那样删除该数据源设置。
所以,现在我们所有的代码如下:
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中执行操作即可。