一个带有多个SqlDataSource的Gridview

时间:2013-06-21 15:57:42

标签: c# asp.net gridview

我有一个gridview,我想用它来使用两个或更多单独的查询。换句话说,我不希望有两个gridview,每个都有一个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>

2 个答案:

答案 0 :(得分:2)

在按钮单击事件中,您可以更改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]事件。

答案 1 :(得分:1)

在每个按钮的Click事件上,就像将GridView1.DataSourceID更改为正确的SqlDataSource的id一样简单。之后可能需要GridView1.DataBind(),但我不确定,因为您使用的是SqlDataSources。首先尝试,然后添加DataBind,如果它不起作用。