I have a GridView
which I am binding using a LinqDataSource
. I want to filter the GridView
based on the value of the userid (which is stored in a Session
). I also have a Session
for determining if the user is an admin.
GridView
to user_id(column) == Session["userid"]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).
Does somebody have any idea?
Here is my aspx:
<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">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="event_id" HeaderText="event_id" ReadOnly="True"
SortExpression="event_id" InsertVisible="False" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="description" HeaderText="description"
SortExpression="description" />
<asp:BoundField DataField="event_start" HeaderText="event_start"
SortExpression="event_start" />
<asp:BoundField DataField="event_end" HeaderText="event_end"
SortExpression="event_end" />
<asp:BoundField DataField="user_id" HeaderText="user_id"
SortExpression="user_id" />
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="DataClassesDataContext" EntityTypeName=""
TableName="calevents" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" Where="user_id = @user_id">
</asp:LinqDataSource>
Some of the things I've tried (Manual databinding):
// 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();
or following, which makes me have to override the edit, cancel and update methods.
// 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 < GridView1.Rows.Count; i++)
{
for (int j = 0; j < 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;
}
}
}
}
you can do something like follwoing...
page_load()
{
if user is not admin put folllowing...
LinqDataSource2.Where = "user_id = @user_id"
else
do not filter
}
So This way you can do this.. at moment since the Admin id is 1 it filter rocord for 1.