Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter GridView depending on userid
    primarykey
    data
    text
    <p>I have a <code>GridView</code> which I am binding using a <code>LinqDataSource</code>. I want to filter the <code>GridView</code> based on the value of the userid (which is stored in a <code>Session</code>). I also have a <code>Session</code> for determining if the user is an admin.</p> <ol> <li>If only user - Filter <code>GridView</code> to user_id(column) == Session["userid"]</li> <li>If admin - Show all user_ids.</li> </ol> <p>I've managed to filter it in numerous ways for the user but not how to later display everything for the admin (admin has the user_id = 1).</p> <p>Does somebody have any idea?</p> <p>Here is my aspx:</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" CausesValidation="False" GridLines="None" AllowPaging="True" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" DataSourceID="LinqDataSource2" DataKeyNames="event_id"&gt; &lt;AlternatingRowStyle CssClass="alt"&gt;&lt;/AlternatingRowStyle&gt; &lt;Columns&gt; &lt;asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /&gt; &lt;asp:BoundField DataField="event_id" HeaderText="event_id" ReadOnly="True" SortExpression="event_id" InsertVisible="False" /&gt; &lt;asp:BoundField DataField="title" HeaderText="title" SortExpression="title" /&gt; &lt;asp:BoundField DataField="description" HeaderText="description" SortExpression="description" /&gt; &lt;asp:BoundField DataField="event_start" HeaderText="event_start" SortExpression="event_start" /&gt; &lt;asp:BoundField DataField="event_end" HeaderText="event_end" SortExpression="event_end" /&gt; &lt;asp:BoundField DataField="user_id" HeaderText="user_id" SortExpression="user_id" /&gt; &lt;/Columns&gt; &lt;PagerStyle CssClass="pgr"&gt;&lt;/PagerStyle&gt; &lt;/asp:GridView&gt; &lt;asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="DataClassesDataContext" EntityTypeName="" TableName="calevents" EnableDelete="True" EnableInsert="True" EnableUpdate="True" Where="user_id = @user_id"&gt; &lt;/asp:LinqDataSource&gt; </code></pre> <p>Some of the things I've tried (Manual databinding):</p> <pre><code> // Registered users may only edit their own events DataClassesDataContext dc = new DataClassesDataContext(); var events = from a in dc.calevents where a.user_id == userId() select a; GridView1.DataSource = events; GridView1.DataBind(); </code></pre> <p>or following, which makes me have to override the edit, cancel and update methods.</p> <pre><code>// Registered users may only edit their own events protected void GridView1_DataBound(object sender, EventArgs e) { if (Session["admin"] != "admin") { for (int i = 0; i &lt; GridView1.Rows.Count; i++) { for (int j = 0; j &lt; GridView1.Columns.Count; j++) { if (GridView1.Rows[i].Cells[j].Text != userId().ToString()) { GridView1.Rows[i].Visible = false; Label1.Text = GridView1.Rows.Count.ToString(); } else GridView1.Rows[i].Visible = true; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload