excluding some rows when databinding

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
 
B

Brock Allen

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.




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

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top