asp.net GridView可以有两个DataSource吗?

时间:2012-05-30 15:36:55

标签: asp.net visual-studio-2010 gridview

下午全部,

我有一个gridview,用于显示数据库中的“操作”列表。

我有一个连接到此gridview的下拉列表,这使用户可以通过下拉列表中的“操作状态”过滤数据('已分配','进入'和'已完成')。这完全正常....

我想要做的是为用户提供另一个过滤器选项,他们还希望按用户名过滤。我为此设置了数据源和下拉列表,但我只能将一个数据源连接到我的gridview?

是否有人建议如何让用户也按用户名和“操作状态”进行过滤?

如果你需要偷看,这是我的代码......

         <asp:SqlDataSource ID="dsActions" runat="server" 
                ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>" 
                SelectCommand="Populate_grdAllActions_Filter" 
                SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddFilterStatus" Name="ActionStatusID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
         </asp:SqlDataSource>

         <asp:SqlDataSource ID="dsActionsByUser" runat="server" 
               ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>" 
               SelectCommand="Populate_grdAllActions_Filter_By_User" 
               SelectCommandType="StoredProcedure">
             <SelectParameters>
                 <asp:ControlParameter ControlID="ddFilterUsers" Name="UserID" 
                     PropertyName="SelectedValue" Type="Int32" />
             </SelectParameters>
         </asp:SqlDataSource>

    <asp:SqlDataSource ID="dsFilterList" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>" 
            SelectCommand="SELECT * FROM [ActionStatus]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="dsFilterUsers" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>" 
            SelectCommand="SELECT * FROM [UserSimpleList]"></asp:SqlDataSource>

    <table style="width: 400px">
        <tr>
            <td colspan="2" style="height: 25px"><b>Filter Options:</b></td>
        </tr>
        <tr>
            <td style="width: 158px">
                Select Action Status:</td>
            <td>
                <asp:DropDownList ID="ddFilterStatus" runat="server"  
                    DataTextField="ActionStatus" DataValueField="ActionStatusID" 
                 AutoPostBack="True" DataSourceID="dsFilterList"></asp:DropDownList>
            </td>
        </tr>
         <tr>
            <td style="width: 158px">
                Select Actions by User:</td>
            <td>
                   <asp:DropDownList ID="ddFilterUsers" runat="server"  
                    DataTextField="UserFullName" DataValueField="UserID" 
                 AutoPostBack="True" DataSourceID="dsFilterUsers"></asp:DropDownList></td>
        </tr>
     </table>

     <br/>

      <asp:GridView ID="grdActions" runat="server" AutoGenerateColumns="False" 
            DataSourceID="dsActions" CssClass="mGrid" 
            PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" 
           AllowPaging="True" PageSize="6" DataKeyNames="ActionID" Width="68%" >
      <AlternatingRowStyle CssClass="alt" />
    <Columns>
          <asp:HyperLinkField DataNavigateUrlFields="ActionID" DataNavigateUrlFormatString="~/UpdateAction.aspx?Edit={0}"
                DataTextField="ActionID" HeaderText="Action ID" >
                <HeaderStyle HorizontalAlign="Center" Wrap="True" />
                <ItemStyle HorizontalAlign="Center" />
            </asp:HyperLinkField>
        <asp:BoundField DataField="Action" HeaderText="Action" 
            SortExpression="Action" >
          <HeaderStyle HorizontalAlign="Center" />
          </asp:BoundField>
        <asp:BoundField DataField="Owner" HeaderText="Owner" 
            SortExpression="Owner">
          <HeaderStyle HorizontalAlign="Center" />
          <ItemStyle Wrap="False" />
          </asp:BoundField>
        <asp:BoundField DataField="TargetDate" HeaderText="Target Date" 
            SortExpression="TargetDate"  DataFormatString="{0:dd-MM-yyyy} " >
          <HeaderStyle HorizontalAlign="Center" Wrap="True" />
          <ItemStyle Wrap="False" />
          </asp:BoundField>
        <asp:BoundField DataField="Action Status" HeaderText="Action Status" 
            SortExpression="Action Status" >
          <HeaderStyle HorizontalAlign="Center" Wrap="True" />
          <ItemStyle Wrap="False" />
          </asp:BoundField>
     </Columns>
       <PagerStyle CssClass="pgr" />
    </asp:GridView>

任何帮助都可以提前获得许多帮助。

此致 贝蒂

2 个答案:

答案 0 :(得分:0)

您可以确定用户选择了哪个选项,然后在代码隐藏中设置数据源,而不是在aspx中将它们连接起来。

Pseudocode假设如果选择了FilterUsers下拉列表,则绑定到Action By User数据源:

if(IsPostBack){
    if(ddlFilterUsers.SelectedValue <> ""){
        grdActions.DataSource = dsActionsByUser;
        dsFilterUsers.DataBind();
        //etc
    }
}

答案 1 :(得分:0)

在代码背后部分,在 ddFilterStatus_SelectIndexChanged()事件中,检查相应的“操作状态”并选择数据源,如下所示,

 if(ddFilterStatus.SelectedValue == someid1)
 {

     grdActions.DataSource =  appropriate datasource

 }
 else
 {
      grdActions.DataSource =  appropriate datasource
  }
  grdActions.DataBind()

希望这会有所帮助......