I followed this question stuck on changing each row color during run-time in listview in asp.net based on database entries and tried to do the same in VB but i am getting some unexplained errors, like Object reference not set to an instance of an object
most likely for this row =>
Dim cell As HtmlTableRow = DirectCast(e.Item.FindControl(“MainTableRow”), mlTableRow)
Please let me know if there is any better way / correct way to do this in VB?
Protected Sub ListView2_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) _
Handles ListView2.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim dataitem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
Dim mstorename As String = DataBinder.Eval(dataitem.DataItem, "Store")
If mstorename = "A1" Then
Dim cell As HtmlTableRow = DirectCast(e.Item.FindControl("MainTableRow"), mlTableRow)
cell.BgColor = #E0E0E0
End If
End If
End Sub
Many thanks for your help.
dk
For this to work, you must ensure that you provide
MainTableRowid totrelement and mark it asrunat="server"i.e. make sure that your mark-up (html) is something likeA different (and IMO, simpler) approach will be using data-binding expressions. For example, in your markup, use
And in code-behind, have a protected function to provide CSS class based on data (a example c# function will be)
And lastly, define those css classes (row, altRow) as per your needs.