question about datagrid

  • Thread starter Thread starter newbie
  • Start date Start date
N

newbie

I've a datagrid (with 4 columns) purely for display purpose on an aspx
page. Is it possible to highlight (or may be change the
backcolor/forecolor)a row based on some condition? For example, if name
= "John Doe", highlight the row else just display.

thanks
 
Try following code in ItemDataBund event:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
DataRowView drv = (DataRowView)e.Item.DataItem;
if(drv["name"].ToString().Equals("John Doe"))
{
e.Item.BackColor = Color.SomeColor;
}
}


HTH

Elton Wang
 
Thank you so much. This worked.

This led me to think of another question. What if I don't want to
display the Name column? That is, let's say I only want to tell the
user whether it's "John Doe" or not. So, while processing, If I find
it's "John Doe", I'll highlight the entire row (w/o actually displaying
any column for name), else I do nothing. I guess the dataBound event
works between the time the rows have been formed and the items are
displayed on the screen. So, I guess, I have to someway remove the Name
column once I've the information as to which row to highlight.

thanks
 
If you want to invisible a cell of bound column, you can use following code.
e.Item.Cells[name_col_index].Visible = false;

Elton
 
Thanks for the response. This works for making a particular "cell"
invisible. But I was trying to ask how to make a whole column
disappear, once I've read the value for that column for each row.

For example, if I have 4 columns - address, phone, age and name, I want
to read each row and based on the value of the name column (say if it's
"John") , I'll highlight the row but I won't render the name column at
all. So I don't want to display the name column at all but I want to
read the value of the name in order to decide whether or not to
highlight. By using Invisible the way you mentioned, the column is
rendered - just the value in the column is made invisible.
 

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

Back
Top