本文介绍了Listview根据单元格值更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表视图,我想根据单元格的值更改行背景色.
我可以修改每个项目的前色属性,但是该行没有运气.
I have a listview and I want to change the row background color based on a cells value.
I am able to modify the forecolor properties of each item but I am having no luck with the row.
Private Sub ListViewTraining_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListViewTraining.ItemDataBound
Dim list As ListView = TryCast(sender, ListView)
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim FormatLabelformating As Label = DirectCast(e.Item.FindControl("FormatLabel"), Label)
Dim ItemNumberLabelformating As Label = DirectCast(e.Item.FindControl("ItemNumberLabel"), Label)
Dim TrainingLabelformating As Label = DirectCast(e.Item.FindControl("TrainingLabel"), Label)
Dim StartDateLabelformating As Label = DirectCast(e.Item.FindControl("StartDateLabel"), Label)
Dim StopDateLabelformating As Label = DirectCast(e.Item.FindControl("StopDateLabel"), Label)
Dim TrainedByLabelformating As Label = DirectCast(e.Item.FindControl("TrainedByLabel"), Label)
Dim UpdateLabelformating As Label = DirectCast(e.Item.FindControl("UpdateLabel"), Label)
Dim UpdatedByLabelformating As Label = DirectCast(e.Item.FindControl("UpdatedByLabel"), Label)
'Dim nRow As System.Web.UI.WebControls.TableRow = DirectCast(e.Item.FindControl("ItemRow"), System.Web.UI.WebControls.TableRow) tried this but I get can't convert to System.Web.UI. Webcontrols
If Not IsNothing(FormatLabelformating) Then
If FormatLabelformating.Text = "h" Then
ListViewTraining.Items(e.Item).BackColor = Color.Aqua
FormatLabelformating.ForeColor = Color.DarkBlue
FormatLabelformating.Font.Bold = True
ItemNumberLabelformating.ForeColor = Color.DarkBlue
ItemNumberLabelformating.Font.Bold = True
TrainingLabelformating.ForeColor = Color.DarkBlue
TrainingLabelformating.Font.Bold = True
StartDateLabelformating.ForeColor = Color.DarkBlue
StartDateLabelformating.Font.Bold = True
StopDateLabelformating.ForeColor = Color.DarkBlue
StopDateLabelformating.Font.Bold = True
TrainedByLabelformating.ForeColor = Color.DarkBlue
TrainedByLabelformating.Font.Bold = True
UpdateLabelformating.ForeColor = Color.DarkBlue
UpdateLabelformating.Font.Bold = True
UpdatedByLabelformating.ForeColor = Color.DarkBlue
UpdatedByLabelformating.Font.Bold = True
Else
FormatLabelformating.ForeColor = Color.Black
FormatLabelformating.Font.Bold = False
ItemNumberLabelformating.ForeColor = Color.Black
ItemNumberLabelformating.Font.Bold = False
TrainingLabelformating.ForeColor = Color.Black
TrainingLabelformating.Font.Bold = False
StartDateLabelformating.ForeColor = Color.Black
StartDateLabelformating.Font.Bold = False
StopDateLabelformating.ForeColor = Color.Black
StopDateLabelformating.Font.Bold = False
TrainedByLabelformating.ForeColor = Color.Black
TrainedByLabelformating.Font.Bold = False
UpdateLabelformating.ForeColor = Color.Black
UpdateLabelformating.Font.Bold = False
UpdatedByLabelformating.ForeColor = Color.Black
UpdatedByLabelformating.Font.Bold = False
End If
End If
End If
End Sub
ASP:
ASP:
<ItemTemplate>
<tr id="ItemRow" runat="server" style="background-color:#FFFFFF;color: #000000; ">
<td style=" width: 50px">
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you certain you want to delete this item?');"
Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="ItemNumberLabel" runat="server" Width="55px"
Text='<%# Eval("ItemNumber") %>' />
</td>
<td>
<asp:Label ID="TrainingLabel" runat="server" Width="255px"
Text='<%# Eval("Training") %>' />
</td>
<td>
<asp:Label ID="StartDateLabel" runat="server" Width="100px"
Text='<%# Eval("StartDate", "{0:dd MMM yyyy}") %>' />
</td>
<td>
<asp:Label ID="StopDateLabel" runat="server" Width="100px"
Text='<%# Eval("StopDate", "{0:dd MMM yyyy}") %>' />
</td>
<td>
<asp:Label ID="TrainedByLabel" runat="server" Width="155px" Text='<%# Eval("TrainedBy") %>' />
</td>
<td>
<asp:Label ID="FormatLabel" runat="server" Width="55px" Text='<%# Eval("Format") %>' />
</td>
<td>
<asp:Label ID="UpdateLabel" runat="server" Text='<%# Eval("Update", "{0:dd MMM yyyy HHmm}") %>' />
</td>
<td>
<asp:Label ID="UpdatedByLabel" runat="server" Text='<%# Eval("UpdatedBy") %>' />
</td>
</tr>
</ItemTemplate>
Any Ideas?
推荐答案
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim dataitem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
Dim tr As System.Web.UI.HtmlControls.HtmlTableRow
tr = DirectCast(dataitem.FindControl("ItemRow"), System.Web.UI.HtmlControls.HtmlTableRow)
tr.BgColor = "#FF0000"
End If
这篇关于Listview根据单元格值更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!