After DataBind in DataGrid set CssClass per row?

R

Randall Parker

Using ASP.Net v1.1.

In a System.Web.UI.WebControls.DataGrid I can't find any method or property that
looks like it would give one access to individual rows after a DataBind.

What I want to do: Set either CssClass or other properties for a table tr tag for
each row and make them different depending on tests of values in each row.

I figure I can use the corresponding table that had a database bound to the grid to
get at values to test. But I do not see how to set properties at a per row level in
the DataGrid. Is this possible?

Is it any more possible in ASP.Net v2.0?

I want to style each individual row in ways dependent on that row's data values. I do
not see how to do that either before or after the DataBind. Is it possible to do at all?
 
E

Eliyahu Goldin

Sure it is possible.

The DataGrid control has Items property. Items represent the rows.

The are 2 standard ways of setting row properties:

1. In PreRender event loop through the myGrid.Items collection.

2. In ItemDataBound event you get access to each individual item, including
the header and footer if any. You get a reference to the item as a parameter
to the event handler.

Eliyahu
 
R

Randall Parker

In the ItemDataBound event I'm trying to get access to the dataset row that was used
to populate that table row.

Is e.Item.DataItem the data row? Or is something else?

I want to look at values in the data row and test them and then decide which css to
assign to that row.

Here's an Item_Bound event routine:

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
//e.Item.Cells[2].Text
//e.Item.Cells[2].CssClass
//e.Item.CssClass
// e.Item.DataItem


}

Also, what are the arguments for the PreRender event? Is there some system for
knowing their args or is there a way to get VS 2003 to generate a method that has the
right args?

I'm guessing it is this:
void PreRender(Object sender,System.EventArgs ee)

But I'm not sure.
 
E

Eliyahu Goldin

Yes, e.Item.DataItem is the data row.You need to typecast it to the
appropriate type.

For the PreRender event info, why don't you look in the MSDN Library? It is
available online if you don't have it locally. You can setup events in the
aspx page Design view. And you guess is almost right. If you setup the event
in the HTML view, the handler should be declared protected.

Eliyahu

Randall Parker said:
In the ItemDataBound event I'm trying to get access to the dataset row
that was used to populate that table row.

Is e.Item.DataItem the data row? Or is something else?

I want to look at values in the data row and test them and then decide
which css to assign to that row.

Here's an Item_Bound event routine:

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
//e.Item.Cells[2].Text
//e.Item.Cells[2].CssClass
//e.Item.CssClass
// e.Item.DataItem


}

Also, what are the arguments for the PreRender event? Is there some system
for knowing their args or is there a way to get VS 2003 to generate a
method that has the right args?

I'm guessing it is this:
void PreRender(Object sender,System.EventArgs ee)

But I'm not sure.


Eliyahu said:
Sure it is possible.

The DataGrid control has Items property. Items represent the rows.

The are 2 standard ways of setting row properties:

1. In PreRender event loop through the myGrid.Items collection.

2. In ItemDataBound event you get access to each individual item,
including the header and footer if any. You get a reference to the item
as a parameter to the event handler.

Eliyahu
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top