简体   繁体   English

如何从带有下拉列表的存储过程中过滤gridview?

[英]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.

相关问题 在asp.net C#中的GridView编辑项目模板中从存储过程中找到下拉列表控件 - Finding drop down list control from a stored procedure in edit item template of gridview in asp.net c# 如何将存储过程变成下拉列表 C# MVC 5 - How to make stored procedure into a drop down list C# MVC 5 如何将下拉列表值作为存储过程的参数传递 - how to pass Drop Down List value as a parameter for stored procedure 如何从下拉列表中显示所选值并将其显示在gridview中 - How to show the selected value from the drop down list and show it in gridview 如何从SQL填充GridView下拉列表选项? - How to populate a GridView drop-down list choices from SQL? 如何编写存储过程,其输入参数是从下拉列表中选择的内容,并将结果传递到空白文本框中? - How to write a stored procedure whose input parameter is a selection from a drop down list & pass the result into a blank text box? 在Gridview中的下拉列表 - Drop Down List In A Gridview 如何在C#中将下拉列表的值传递给sqlserver中的存储过程 - How to pass value of drop down list to stored procedure in sqlserver in C# 如何使用存储过程填充为搜索记录选择的下拉列表 - How to populate drop down list selected for search record using stored procedure 如何根据从下拉列表中选择的项目调用oracle存储过程 - How to call oracle Stored procedure based on the item selected from drop-down
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM