Urgent Help Required from Gurus - Conditional databinding

C

CGuy

Hi,

I have an ASPX page which has a datagrid and this datagrid is bound to a
Custom Collection.

sample code

this.DataGrid1.DataSource = UserManager.Users;
this.DataGrid1.DataBind();

Everything works fine and when the page is loaded, the datagrid displays
the list of users present in the Usermanager.Users object.

Now, I want to do a conditional binding - for example, I want the
datagrid to have only those users who have certain privileges. To be more
specific, this is what I want to do

foreach(User user in UserManager.Users)
{
if(user.Privileges.Contains(certainPrivilege) ||
user.Privileges.Contains(anotherCertainPrivilege)
{
//Add the row to the grid
}
else
{
//Do not add this row to the grid
}
}

How do I accomplish this - please note that I don't want to the change the
datasource of the grid to reflect this - the data source should be the
entire list of Users and not a filtered list based on priviliges.

Please help me with this - what I am looking for is a solution which will
allow me to use the ItemDataBound or DataBinding events to do this.

Also, I do not want to hide the rows (visible = false). This spoils the
paging routines in the page.

CGuy
 
I

Ignacio Machin

Hi CGue,

Hi , unfortunally the DataGridItemCollection does not provide any method
to add or delete items, therefore you cannot add or remove rows in the grid.

I would strongly suggest you that the best way to do this is filtering the
data source and bind the grid to the filtered collection. if you don't want
to do this for some weird reason then the only alternative that I see is
customize the datagrid class by yourself, this is by far more complex , and
pointless in this case, that filtering a collection.

Hope this help,
 
S

Sean

Use a Datagrid.Table["yourtable"].select (kinda simalar to a SQL select)
statement to populate a DataRow array. You can then point your datagrid to
that as your datasource.
 
J

John Timney \(Microsoft MVP\)

You cannot programmatically add to or remove elements from the Items
collection, which is a collection that is created at the time of
databinding. The best you can do is manipulate the data in the ItemCreated
event which gives you access to this bound collection - even if you set the
data to contain nothing the entry will still exist, and a grid position
would likely still occur. At the stage of ItemDataBound the actual
DataBinding has occured so in reality its already too late to do what you
want.

What you could probably do is provide a dataview of the data in the dataset
and have that filter out what you dont want to bind, and then bind it - this
is called a row filter and is probably more akin to what you require.
Alternatively, take your dataset and clone a new dataset manually at runtime
from the origonal dataset, and bind to that instead.

I would suggsetd you have a read of this:
http://msdn.microsoft.com/msdnmag/issues/01/07/cutting/default.aspx

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top