Dynamically generated content in template for given row in GridView, how?
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.
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");
精彩评论