2013-06-21 43 views
0

我有一個gridview,我想使用兩個或多個單獨的查詢。換句話說,我不希望有兩個gridviews與他們每個人與sqldatasource。一個GridView與多個SqlDataSources

如何只有一個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> 

回答

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

我假設你加入下拉列表收集像[選項與選項2] 兩個項目不要忘記讓[DropDownList1.AutoPostBack = 「真」 ],否則它不會觸發[SelectedIndexChanged]事件。

+0

您是否知道如何使用Dropdownlist – Apollo

+0

請參閱後期修改。 – Zeeshanef

0

上的每個按鈕的Click事件,它是作爲改變GridView1.DataSourceID正確的SqlDataSource的ID一樣簡單。之後可能需要GridView1.DataBind(),但我不確定,因爲您使用的是SqlDataSources。先不嘗試,然後添加DataBind,如果它不起作用。