简体   繁体   中英

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: 在此处输入图片说明

I want to add a custom dropdown list that has the numbers 1,5,7 to filter this data by minimum number of animals. 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:

     <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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM