Wednesday, 11 June 2008

DynamicData - Limit Data Displayed By User

Articles in this Series

Limiting Data by the Current User

This is a really simple scenario where the table has a column which is update with the current user name at record creation (see: Automatic Column Update how to automatically update tables) all that is required is to add a WhereParameter to the pages LinqDataSources. In the Page_Load add the line for each LinqDataSource on the page (this line of code is for the List.aspx page).

GridDataSource.WhereParameters.Add(new Parameter("CreatedBy", DbType.String, User.Identity.Name));
Obviously you may whish to add a CheckBox or RadioButton to toggle this feature or even add a filter to filter several users at once (see Marcin Dobosz Blog post Dynamic Data samples: Extending the FilterRepeater for details on how to extend the FilterRepeater)

14 comments:

Anonymous said...

Hi,

I appreciate your postings. I need to filter data by 2 user_code (current user and global user). Using WhereParamers.Add the column name needs to be unique. Using the GridDataSource.Where also mbreaks the DynamicFilters. Do you know a way around this?

regs,
Nic

Stephen J. Naughton said...

Hi have a look at this project on Codeplex http://www.codeplex.com/DynamicDataFiltering I think this will do what you want. And i will be doing a series on this soon :D

Anonymous said...

Steve,
I know that when you set "ScaffoldColumn(false)", you can hide the column in the display but can this be done conditionally ? For example, I have a field called "DeletedFlag" and when the action is Insert, I want this field hidden but when the action is Edit, I want the field displayed.

Thanks
fxl

Stephen J. Naughton said...

Hi have a look at this article here: http://csharpbits.notaclue.net/2008/10/dynamic-data-hiding-columns-in-selected.html

Steve :D

Manish Jain said...

What if instead of equality I want to check Greater than or less than values... it will not work..

Stephen J. Naughton said...

Hi there Manish, I would recommend DD4 with VS2010 as that support QueryExtenders which allow much greater flexibility.

Steve

Anonymous said...

GridDataSource.WhereParameters.Add("UserId", DbType.Int32, "3");

throw that error

No property or field 'UserId1' exists in type 'Web'

Anonymous said...

I think the filterrepater using that parameter.

Stephen J. Naughton said...

Hi Anonymous there must be a property on the entity and with that type, otherwise you will get that error.

Steve :)

Anonymous said...

Hi Steve. Thanks for your quick reply.
I m just wondering is that possible? context based filter.

// Filter The Context
DBCntxt.Webs.UserId = "3";
DBCntxt.Comments.UserId "3";
DBCntxt.Products.UserId "3";

// Register The Context
model.RegisterContext(typeof(DBCntxt), new ContextConfiguration() { ScaffoldAllTables = true
});

I know, i can't use like this but is there any way to do this?

Stephen J. Naughton said...

Hi, again :) I'm not that deep into L2S or EF to know sorry, haqve you any links to where you got that and I'll investigate it and do an article on it. (I'll credit you of course)

Steve :D

Anonymous said...

Hi :). This is my idea. There is no article, link or anything about this question. I have searched the internet and can't find anything about this question. I think there is no solution for this :).
by the way my name is Oguz. Very nice to meet you.

Stephen J. Naughton said...

Hi Oguz, I'll have a look into it.

Steve :)

Anonymous said...

I will be eagerly waiting :)

Thanks.