This is a migrated thread and some comments may be shown as answers.

Filter RadGrid by Current User by default

6 Answers 78 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carolee
Top achievements
Rank 1
Carolee asked on 10 Sep 2012, 06:31 PM
If I have a large list of assignments or orders in my SQL database, is there a way to present the logged-in user with only those which are his or hers, by default when they land on the page?  Or, I know there IS a way... But can someone give me explicit details on how to accomplish this?

Thank you  so much.

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Sep 2012, 07:17 PM
Hello,

Please write below code snippet in needdatasource event.

if(!ispostback)
{
 
RadGrid1.MasterTableView.FilterExpression = "([UserName] LIKE \'%" + "YOURLOGINUSERNAME" + "%\') ";
 
            GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("UserName"); // Column unique name
            column.CurrentFilterFunction = GridKnownFunction.Contains;
            column.CurrentFilterValue = "YOURLOGINUSERNAME";
 
}


Thanks,
Jayesh Goyani
0
Carolee
Top achievements
Rank 1
answered on 10 Sep 2012, 07:23 PM
Hello Jayesh,
Wow...Does this solution dynamically retrieve and apply the LoginName of the user who comes to the page? 
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Sep 2012, 07:28 PM
Hello,

No you have to get your logged in username by your own way.

Once you get username you can able to replace with "YOURLOGINUSERNAME" in above code.

Thanks,
Jayesh Goyani
0
Casey
Top achievements
Rank 1
answered on 10 Sep 2012, 07:38 PM
Hi Carolee,

Another way to do this would be to pass the user ID to the method selecting the records, and only select the applicable records with the query. I think it would save some processing time due to the method return only the applicable rows, rather than all of the rows being returned, and then filtered by the RadGrid.

I hope this helps!
Casey


0
Carolee
Top achievements
Rank 1
answered on 10 Sep 2012, 07:41 PM
Hi Casey,

That sounds like a plan.... But I am so green I don't know how to go about that... I don't suppose you could point me to an example?

Thanks so much!
0
Casey
Top achievements
Rank 1
answered on 10 Sep 2012, 07:57 PM
Hi Carolee,

Well, in our apps we use Forms authentication mode, and I'm able to get the user ID of the person logged in by referencing "HttpContext.Current.User.Identity.Name" in the code behind. Based on the following forum, User.Identity.Name, this is not set unless you use FormsAuthentication.SetAuthCookie or FormsAuthentication.RedirectFromLoginPage.

If you are not currently doing that, then you could just set a session variable and assign the user name to it when they login. And then in the NeedDataSource of your RadGrid, you could pass Session["userName"].ToString() to the method.

I hope this helps!
Casey

protected void LoginButton_Click(object sender, EventArgs e)
{
        Session.Add("userName", TextBox1.Text);
}

protected  void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
     RadGrid1.DataSource = YourClass.YourMethod(Session["userName"].ToString());
}


Tags
Grid
Asked by
Carolee
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Carolee
Top achievements
Rank 1
Casey
Top achievements
Rank 1
Share this question
or