[英]how to filter a gridview from a stored procedure with a drop down list?
i have been stuck on this problem for hours and can not seem to figure it out. 我已经在这个问题上停留了几个小时,似乎无法弄清楚。 I have a gridview I populated from a sql data source taht gives me this table:
我有一个从sql数据源填充的gridview taht给了我这张表:
I want to add a custom dropdown list that has the numbers 1,5,7 to filter this data by minimum number of animals. 我想添加一个自定义下拉列表,其数字为1,5,7,以按最小动物数量过滤此数据。 heres the code for the drop down.
这是下拉菜单的代码。
Minimum Animals:
<asp:DropDownList ID="ddlMinimumAnimals" runat="server"
AutoPostBack = "True" AppendDataBoundItems = "True">
<asp:ListItem Text = "1" Value = "1"></asp:ListItem>
<asp:ListItem Text = "5" Value = "1"></asp:ListItem>
<asp:ListItem Text = "7" Value = "1"></asp:ListItem>
</asp:DropDownList>
I just want that when i click the dropdown it filters the table to the minimum, all the tutorials I have found do not use a stored procedure so I am not sure how to bind the data. 我只是希望当我单击下拉列表时,它将表格过滤到最小,我发现的所有教程都没有使用存储过程,所以我不确定如何绑定数据。
Gridview Code: Gridview代码:
<asp:GridView ID="GridView2" class="grids" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" AllowSorting="True" AllowPaging="True">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Animals" HeaderText="Animals" SortExpression="animals" />
<asp:BoundField DataField="Company" HeaderText="Company" SortExpression="company" />
</Columns>
</asp:GridView>
Can't really tell if you want to add a custom dropdown list to the grid or to the form containing the grid. 无法真正确定您是否要向网格或包含网格的表单添加自定义下拉列表。 You can create custom fills on your data adapter but if it were me I'd dump the data to a datatable and either run a linq query against it to get my desired values based on the dropdown or I'd use a DataView and dump the data back into the original table once filtered.
您可以在数据适配器上创建自定义填充,但如果是我,则将数据转储到数据表中,或者对它运行linq查询以基于下拉列表获取所需的值,或者使用DataView并转储数据一旦过滤就回到原始表中。 Good luck.
祝好运。
You're going to need to write some code to modify your data source and hook the two controls together. 您将需要编写一些代码来修改您的数据源并将两个控件挂钩在一起。 The fact that it is coming from a stored procedure makes no difference.
它来自存储过程的事实没有区别。
Controls: 控制项:
<asp:DropDownList ID="ddlMinimumAnimals" runat="server" AutoPostBack = "True">
<asp:ListItem Text = "1" Value = "1"></asp:ListItem>
<asp:ListItem Text = "5" Value = "1"></asp:ListItem>
<asp:ListItem Text = "7" Value = "1"></asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView2" class="grids" runat="server" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Animals" HeaderText="Animals" SortExpression="animals" />
<asp:BoundField DataField="Company" HeaderText="Company" SortExpression="company" />
</Columns>
</asp:GridView>
Basic code (in code-behind or server script tags): 基本代码(在代码隐藏或服务器脚本标签中):
'Im assuming your going to run this on every page load'
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack() Then
'use this number to limit your query via SQL-TOP-Clause or Limit or whatever'
Dim minAnimals As Integer = CInt(ddlMinimumAnimals.SelectedValue)
'get your data here ... lets assume you call it "yourData"'
' then bind your data to the gridview.'
GridView2.DataSource = yourData
GridView.DataBind()
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.