Per aggiungere un BoundField in codice dietro (questo è VB, ma simile per C#) Ricerche:
bf = New BoundField()
bf.DataField = "FieldName"
bf.HeaderText = "Header"
bf.SortExpression = "FieldName(could be different)"
bf.ItemStyle.CssClass = "NoWrap"
MyGrid.Columns.Add(bf)
Se si vuole rendere i dati CssClass dipendente si avrebbe bisogno di un TemplateField Esempio:
tf = New WebControls.TemplateField()
tf.HeaderText = "Whatever"
tf.SortExpression = "Whatever"
tf.ItemTemplate = New MyItemTemplate("DataField", "CssDataField")
AssessmentGrid.Columns.Add(tf)
MyItemTemplate ITemplate implementa nella cartella App_Code esempio:
Imports Microsoft.VisualBasic
Public Class MyItemTemplate
Implements System.Web.UI.ITemplate
'Normally Template type would be in here but we are only do Item
'(no edit, delete or header etc)
Dim DataField1 As String 'Displayed data
Dim DataField2 As String 'CssClass
Sub New(ByVal Field1 As String, ByVal Field2 As String)
DataField1 = Field1
DataField2 = Field2
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements System.Web.UI.ITemplate.InstantiateIn
Dim ml As New Label()
ml.ID = DataField1
ml.Text = ""
ml.CssClass = ""
AddHandler ml.DataBinding, New EventHandler(AddressOf Item_DataBinding)
container.Controls.Add(l)
End Sub
Protected Sub Item_DataBinding(ByVal sender As Control, ByVal e As System.EventArgs)
Dim bound_value_object As Object
Dim data_item_container As IDataItemContainer = sender.NamingContainer
Dim Parent As TableCell = sender.Parent
Dim l As Label = sender
bound_value_object = DataBinder.Eval(data_item_container.DataItem, DataField1)
l.Text = bound_value_object.ToString
bound_value_object = DataBinder.Eval(data_item_container.DataItem, DataField2)
Parent.CssClass = bound_value_object.ToString
End Sub
End Class
Si potrebbe applicare CssCl ass all'etichetta diretta, ma la domanda originale l'aveva sulla cella
fonte
2015-04-08 03:20:04