更改基于模板列的GridView行的颜色,不受管制
问题描述:
我的GridView控件不使用的控制,因为它是填充使用表达式更改基于模板列的GridView行的颜色,不受管制
<asp:TemplateField HeaderText="As Of Sales">
<ItemTemplate>
<%#Getsales(Decimal.Parse(Eval("asofsales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTemplate>
<%#Getsales1().ToString("C0")%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
</asp:TemplateField>
我想比较列索引1和列索引8,如果8更大然后1它应该是一个不同的字体颜色。
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim x As String
x = e.Row.Cells(1).Text
Dim y As String
y = e.Row.Cells(8).Text
If Convert.ToInt32(x) <= Convert.ToInt32(y) Then
e.Row.ForeColor = System.Drawing.Color.Blue
End If
End If
End Sub
答
这里的东西,你可以尝试
在GridView
<asp:GridView runat="server" ID="grdv" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="T1">
<ItemTemplate>
<%# Eval("T1")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="T2">
<ItemTemplate>
<%# Eval("T2")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim d As New DataTable
d.Columns.Add("T1")
d.Columns.Add("T2")
d.Rows.Add(1, 2)
grdv.DataSource = d
grdv.DataBind()
End Sub
Private Sub grdv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdv.RowDataBound
Dim data As DataRowView = e.Row.DataItem
If data Is Nothing Then Exit Sub
If e.Row.RowType = DataControlRowType.DataRow Then
If data.Item("T1") <= data.Item("T2") Then e.Row.ForeColor = Color.Red
End If
End Sub
这应该结合一个DataTable工作。如果您使用的是集合,那么RowDataBound事件将需要稍微改变。
+0
谢谢你,完美的工作 – MyHeadHurts 2011-02-28 14:08:25
+0
很高兴,帮助 – 2011-02-28 14:41:00
那么你发布的代码的实际问题是什么? – TheGeekYouNeed 2011-02-25 22:14:52
多数民众赞成在我想什么时,我写了它,但它并没有比较两列,因为颜色不变 – MyHeadHurts 2011-02-26 14:23:37