控制Gridview基于多个下拉列表的选择

时间:2013-03-23 01:18:06

标签: c# .net database

我创建了一个名为Ztv.mdf的数据库,表格为tabl1,包含[Name],[fullname],[Id],[dept]&也 为用户创建了一个下拉列表1当用户从下拉列表中选择的部门时,gridview显示输出。它工作正常。但是我想为Name.my目标提供另外一个下拉列表,当用户从dropdownlist1和amp中选择值时;下拉列表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>

1 个答案:

答案 0 :(得分: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