控制网格视图基于多个下拉列表的选择

本文关键字:选择 下拉列表 于多个 视图 控制网格 | 更新日期: 2023-09-27 18:05:19

我创建了一个名为Ztv的数据库。mdf, table as tab1 with [Name], [fullname], [Id],[dept]&也为dept创建了一个下拉列表1,当用户从下拉列表1中选择dept值时,gridview显示输出。它工作得很好。但我想多一个下拉列表的名称。我的目标是,当用户从两个dropdownlist1 &下拉列表2控制网格视图。我该怎么做呢?

我的代码为1下拉列表&网格视图是:

<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="ddlSearchCriteria" runat="server" 
        DataSourceID="SqlDataSource1" DataTextField="depat" DataValueField="Id" 
        AutoPostBack="True">
    </asp:DropDownList>
    &nbsp;&nbsp;
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ztvConnectionString %>" 
        SelectCommand="SELECT [Id], [depat] FROM [Table1]"></asp:SqlDataSource>
    <br />
    <asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Id" DataSourceID="SqlDataSource2" CellPadding="4" 
        ForeColor="#333333" GridLines="None" style="margin-left: 155px">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="fullname" HeaderText="fullname" 
                SortExpression="fullname" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ztvConnectionString %>" 
        SelectCommand="SELECT [Name], [fullname], [Id] FROM [Table1] WHERE ([Id] = @Id)">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlSearchCriteria" Name="Id" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <br />
    <br />
</div>
</form>

控制网格视图基于多个下拉列表的选择

您可以使用存储过程通过多个下拉列表选择将数据绑定到网格视图。将下拉选择的值作为参数传递给存储过程并获得结果。我已经这样实现了。

 public void BindGridList(object sender, EventArgs e)
    {
        try
        {
            using (CompMSEntities1 CompObj = new CompMSEntities1())
            {
                DateTime Start = Convert.ToDateTime(txtStart.Text);
                DateTime End = Convert.ToDateTime(txtEnd.Text);
                Int32 Department = Convert.ToInt32(ddlDept.SelectedValue);
                Int32 Category = Convert.ToInt32(ddlCategory.SelectedValue);
                Int32 Priority = Convert.ToInt32(ddlPriority.SelectedValue);
                Int32 Status = Convert.ToInt32(ddlStatus.SelectedValue);
                GridViewComplaintReport.DataSource = CompObj.SP_ManageComplaint_Summary(Start, End, Department, Category, Priority, Status);
                SP_ManageComplaint_Summary_Result obj = new SP_ManageComplaint_Summary_Result();
                GridViewComplaintReport.DataBind();
                GridViewComplaintReport.Visible = true;
                ExportTable.Visible = true;
                TableGrid.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblException.Text = "Problem in data retrive from database .";
            lblException.Visible = true;
            ErrorHandler.WriteError(ex.ToString());
        }

存储过程代码-

CREATE PROCEDURE  dbo.SP_ManageComplaint_Summary

  @StartDate datetime,
  @EndDate datetime,
  @DepartmentID INT,
  @CategoryID INT,
  @PriorityID INT ,    
  @StatusID INT
AS
BEGIN
  SELECT d1.DepartmentName,c1.CategoryName,s1.StatusType,
  p1.PriorityType,c.Title, c.[Description], c.Created,c.DueDate,
  u1.Email, u1.UserID, c.QMRClosedDate
  FROM  Complaint   c   
  Inner  JOIN Department  d1 ON c.DepartmentID = d1.DepartmentID
  Inner JOIN Category  c1  ON c.CategoryID = c1.CategoryID
  Inner JOIN Priority  p1  on c.PriorityID = p1.PriorityID
  Inner JOIN UserInfo   u1 on c.UserID = u1.UserID   
  Inner JOIN [Status]  s1 on c.StatusID = s1.StatusID   

  WHERE 
 c.Created  BETWEEN @StartDate AND @EndDate+1   AND

     d1.DepartmentID = Case when @DepartmentID = -1 then d1.DepartmentID else @DepartmentID end AND        
     c1.CategoryID = Case when @CategoryID = -1 then  c1.CategoryID else @CategoryID end AND
     p1.PriorityID = Case when @PriorityID = -1 then p1.PriorityID  else @PriorityID end AND
     s1.StatusID =Case When @StatusID = -1 then s1.StatusID else @StatusID end