views:

15

answers:

1

Hi guys.

I have data on SQL server like this:

ItemID Quantity
  1       3
  2       0
  3       7

I would like to display that data in GridView using templates. The thing is that instead of Quantity in numbers I would like to display text:

Green text saying "item on stock" when Quantity > 0

Red text saying "item unavailable" when Quantity = 0

My question is, how should I implement such functionality? How to generate such HTML tag dynamically and add it to the template?

Thanks for your time.

A: 

You could check the value in the row data bound event and set the label in the template.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx


This is making the assumption your gridview is bound to a datatable (might fail if it is bound to an object array, for example, I'm not certain):

//To get bound data
DataRowView rowView = (DataRowView)e.Item.DataItem;
object value = rowView["columnName"];

//To get a control
TextBox txtName = (TextBox)e.Item.FindControl("txtName");
wllmsaccnt
Thanks, I've rad the article. But the article says about modyfying the Cells property. How do I get reference to the template for current cell? Or for current row I guess?
Wodzu
Should I build my template at the occurence of the event and assign it via: e.Row.TemplateControl?
Wodzu