For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > March 2005 > excluding some rows when databinding









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author excluding some rows when databinding
Andy Fish

2005-03-31, 4:01 pm

Hi,

Say I have an Array of "Person" objects that I'm using as the DataSource for
a grid control. At databinding time I'd like to select only people with
age>18 to go into the grid and ignore all the others.

The only way I can think is to make a new array (or arraylist) containing
only those I want to display but this seems a bit cumbersome. Is there any
simple way to do this (for example in the ItemDataBound event)?

I guess I could write a custom enumerator but that sounds like even more
work than copying the array.

TIA

Andy


Brock Allen

2005-03-31, 4:01 pm

You can handle the DataGrid's RowDatabound event and do something like this:

private void _grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if (((Person)e.Item.DataItem).Age > 18)
{
e.Item.Visible = false;
}
}
}

e.Item is the DataGridRow in the DataGrid. e.Item.DataItem is the row/object
that it was data bound to -- Person in your case. So inspect the Person object
then set the DataGridRow's Visible to false.

-Brock
DevelopMentor
http://staff.develop.com/ballen



> Hi,
>
> Say I have an Array of "Person" objects that I'm using as the
> DataSource for a grid control. At databinding time I'd like to select
> only people with
>

age>> 18 to go into the grid and ignore all the others.
age>>
> The only way I can think is to make a new array (or arraylist)
> containing only those I want to display but this seems a bit
> cumbersome. Is there any simple way to do this (for example in the
> ItemDataBound event)?
>
> I guess I could write a custom enumerator but that sounds like even
> more work than copying the array.
>
> TIA
>
> Andy
>




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com