Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:17:41+00:00 2026-06-11T06:17:41+00:00

I have a GridView which I am binding using a LinqDataSource . I want

  • 0

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.

  1. If only user – Filter GridView to user_id(column) == Session[“userid”]
  2. If admin – Show all user_ids.

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;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:17:42+00:00Added an answer on June 11, 2026 at 6:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview which I am binding through code behind, I want to
Have a gridview control which will show X amount of rows. I want to
i have a GridView (selectable) in which I want to generate a dynamic GridView
I am using Linq-to-SQL. Currently I am binding gridview through linq which query written
I have a GridView which is continually rebound using a timer and is within
I have a GridView with an EditItemTemplate, to which I am binding a Dictionary
I have a GridView in which I want to embed a checkbox into the
I have a GridView which is programatically filled from the DB (not a SqlDataSource
I have a GridView which I am populating by calling a method to return
I have a GridView which is databound to an XML Datasource. For one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.