Apollo
Apollo

Reputation: 2070

One Gridview with Multiple SqlDataSources

I have one gridview that I want to use two or more separate queries with it. In other words I dont want to have Two gridviews with each of them with a sqldatasource.

How can I have just one gridview and use multiple SqlDataSources? For example if I have two buttons. When one is clicked use one DataSource and when the other one is clicked used the other DataSource while using the same 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>

Upvotes: 1

Views: 2319

Answers (2)

Zeeshanef
Zeeshanef

Reputation: 709

On your button click event you can change select commands of the 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();

Edit: With dropdownlist:

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();
    }
}

I assume you added two items in dropdownlist collection like [Option1 and Option2] don't forget to make [DropDownList1.AutoPostBack="True"] otherwise it will not fire the [SelectedIndexChanged] event.

Upvotes: 2

Garrison Neely
Garrison Neely

Reputation: 3289

On the Click event of each button, it is as simple as changing GridView1.DataSourceID to the id of the correct SqlDataSource. It may require a GridView1.DataBind() afterwards, but I'm not sure since you're using SqlDataSources. Try without first, then add DataBind if it doesn't work.

Upvotes: 1

Related Questions