2015-11-03 57 views
0

我有一個asp.net應用程序有一個gridview的所有用戶,但我不想'管理員'用戶顯示給用戶。不顯示指定的值從SQL返回在Asp.net WebForm GridView

HTML

<asp:GridView ID="tblUsers" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlUsers" GridLines="None" Width="15%"> 
     <AlternatingRowStyle BackColor="White" /> 
     <Columns> 
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
     </Columns> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <RowStyle BackColor="#EFF3FB" /> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlUsers" runat="server" ConnectionString="<%$ ConnectionStrings:PaydayLunchConnectionString1 %>" SelectCommand="SELECT [Name] FROM [Users]"></asp:SqlDataSource> 

我沒有後面的代碼爲這個在所有。我是否需要某種存儲過程或某些廣告,如果是這樣,我如何編寫它作爲存儲過程並不是我的強項。

回答

1

你需要流向何方cluase添加到像SELECT [名]您的選擇命令FROM [用戶]其中,[名]!= '管理員' 或選擇[名稱] FROM [用戶] Where [fieldname]!='Admin'。見下面的代碼。

<asp:GridView ID="tblUsers" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlUsers" GridLines="None" Width="15%"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
    </Columns> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <RowStyle BackColor="#EFF3FB" /> 
</asp:GridView> 
<asp:SqlDataSource ID="SqlUsers" runat="server" ConnectionString="<%$ ConnectionStrings:PaydayLunchConnectionString1 %>" SelectCommand="SELECT [Name] FROM [Users] Where [name] != 'Admin'"></asp:SqlDataSource> 
+0

不能相信我忘了我有'SelectCommand',否則我不需要提出問題 – murday1983

0

只需在您的SelectCommand附加一個where子句。

HTML

<asp:GridView ID="tblUsers" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlUsers" GridLines="None" Width="15%"> 
     <AlternatingRowStyle BackColor="White" /> 
     <Columns> 
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
     </Columns> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <RowStyle BackColor="#EFF3FB" /> 
    </asp:GridView> 
    <asp:SqlDataSource 
     ID="SqlUsers" 
     runat="server" 
     ConnectionString="<%$ ConnectionStrings:PaydayLunchConnectionString1 %>" 
     SelectCommand="SELECT [Name] FROM [Users] WHERE [name] <> 'Admin'"></asp:SqlDataSource>