【问题标题】:One Gridview with Multiple SqlDataSources一个具有多个 SqlDataSources 的 Gridview
【发布时间】:2013-06-21 15:57:42
【问题描述】:

我有一个gridview,我想使用两个或多个单独的查询。换句话说,我不想有两个gridviews,每个gridviews都有一个sqldatasource。

我怎样才能只有一个gridview 并使用多个SqlDataSources?例如,如果我有两个按钮。单击一个时使用一个 DataSource,单击另一个时使用另一个 DataSource,同时使用相同的 gridview。

<div id ="Div1" runat ="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333" 
        GridLines="None" Width="100%" AllowPaging="True" AllowSorting="True" 
            PageSize="20">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="ItemDesc" HeaderText="Item Description" 
                SortExpression="ItemDesc" />
            <asp:BoundField DataField="TypeDesc" HeaderText="Type" 
            SortExpression="TypeDesc" >
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="Total" HeaderText="Total" 
                SortExpression="Total" >
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>
    <%--Query to get total --%>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TLineConnectionString %>" 
        SelectCommand="SELECT ItemDesc, TypeDesc, Total FROM vwTotal WHERE Admin = 0 ORDER BY Total DESC">            
    </asp:SqlDataSource>
     <%--Query to get total Admin only--%>
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:LineConnectionString %>" 
        SelectCommand="SELECT ItemDesc, TypeDesc, Total FROM vwTotal WHERE Admin = 1 ORDER BY Total DESC">            
    </asp:SqlDataSource>
</div>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    在您的按钮单击事件中,您可以更改 SqlDataSource1 的选择命令。

    //Button1 Click
    SqlDataSource1.SelectCommand="SELECT ItemDesc, TypeDesc, Total FROM vwTotal WHERE Admin = 0 ORDER BY Total DESC";
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    
    //Button2 Click
    SqlDataSource1.SelectCommand="SELECT ItemDesc, TypeDesc, Total FROM vwTotal WHERE Admin = 1 ORDER BY Total DESC";
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    

    编辑: 带下拉列表:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selindex = DropDownList1.SelectedIndex;
    
        if (selindex == 0) //Option1 selected
        {
            SqlDataSource1.SelectCommand = "SELECT ItemDesc, TypeDesc, Total FROM vwTotal WHERE Admin = 0 ORDER BY Total DESC";
            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();
        }
    
        else if (selindex == 1) //Option2 selected
        {
            SqlDataSource1.SelectCommand = "SELECT ItemDesc, TypeDesc, Total FROM vwTotal WHERE Admin = 1 ORDER BY Total DESC";
            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();
        }
    }
    

    我假设您在下拉列表集合中添加了两个项目,例如 [Option1 和 Option2] 不要忘记制作 [DropDownList1.AutoPostBack="True"] 否则它不会触发 [SelectedIndexChanged] 事件。

    【讨论】:

    • 你知道如何使用 Dropdownlist 来做到这一点
    【解决方案2】:

    在每个按钮的 Click 事件上,只需将 GridView1.DataSourceID 更改为正确的 SqlDataSource 的 id 即可。之后可能需要GridView1.DataBind(),但我不确定,因为您使用的是SqlDataSources。先不试试,不行再加DataBind。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2016-09-28
      • 2010-11-26
      相关资源
      最近更新 更多