0

我创建了一个名为 Ztv.mdf 的数据库,表为 tabl1,带有 [Name], [fullname], [Id],[dept]& 还为 dept 创建了一个下拉列表 1。当用户从下拉列表 1 中选择了 dept 值时gridview 显示输出。它工作正常。但我想要 1 个名称的下拉列表。我的目标是,当用户从下拉列表 1 和下拉列表 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>

4

1 回答 1

0

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

 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 
于 2013-03-23T07:29:16.037 回答