excluding some rows when databinding

  • Thread starter Thread starter Andy Fish
  • Start date Start date
A

Andy Fish

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
 
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>>
 
Back
Top