开发者

change gridview row color based on templatefields without controls

开发者 https://www.devze.com 2023-02-13 07:15 出处:网络
My gridview does not use controls, because it is populated using expressions <asp:TemplateField HeaderText=\"As Of Sales\">

My gridview does not use controls, because it is populated using expressions

<asp:TemplateField HeaderText="As Of Sales">
<ItemTemplate>
 <%#Getsales(Decimal.Parse(Eval("asofsales").ToString())).ToString("C0")%>
</ItemTemplate>
<FooterTempl开发者_如何学编程ate>
<%#Getsales1().ToString("C0")%>
</FooterTemplate>
    <FooterStyle Font-Bold="True" />
</asp:TemplateField>

I want to compare column index 1 and column index 8, and if 8 is bigger then 1 it should be a different font color.

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


Here is something that you can try

The 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>

Code Behind

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

This should work for binding to a DataTable. If you're using a collection then the RowDataBound event will need changing slightly.

0

精彩评论

暂无评论...
验证码 换一张
取 消