嵌套gridview父行颜色的问题

时间:2014-03-31 05:19:02

标签: asp.net vb.net gridview

我正在尝试根据数据设置父网格视图中的行颜色。例如,在编码中,如果货币是美元然后将行设置为巧克力颜色,则将其设置为红色,而是我的程序显示其全部为红色,即使在gridview中存在货币USD数据。

继承我的代码,

Protected Sub gvUserInfo_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        e.Row.Cells(1).Visible = False
        Dim currency As String = e.Row.Cells(3).Text
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("HedgingTestConnectionString").ConnectionString)
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            For Each cell As TableCell In e.Row.Cells
                If currency = "USD" Then
                    cell.ForeColor = Color.Chocolate
                Else
                    cell.BackColor = Color.Red
                    Label1.Text = e.Row.Cells(3).Text
                End If
            Next
            con.Open()
            Dim gv As GridView = DirectCast(e.Row.FindControl("gvChildGrid"), GridView)
            Dim ref As Integer = Convert.ToInt32(e.Row.Cells(1).Text)
            Dim cmd As New SqlCommand("select * from TT where Reference_NO=" & ref, con)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            da.Fill(ds)

            con.Close()
            gv.DataSource = ds
            gv.DataBind()
        End If
    End Sub

以下是我的gridview代码,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Hedging_ID,CCY"        HeaderStyle-Font-Size="Medium" HeaderStyle-BackColor="SeaGreen" OnRowDataBound="gvUserInfo_RowDataBound" width="100%"  
        HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" GridLines="None">
        <Columns>
            <asp:TemplateField ItemStyle-Width="20px">
            <ItemTemplate>
            <a href="JavaScript:divexpandcollapse('div<%# Eval("ref_ID")%>');">
            <img id="imgdiv<%# Eval("ref_ID")%>" border="0" src="plus.png" />
            </a>
            </ItemTemplate>
            <ItemStyle Width="20px"></ItemStyle>
            </asp:TemplateField>


            <asp:BoundField DataField="ref_ID" HeaderText="ID" SortExpression="ref_ID" />
            <asp:BoundField DataField="Bank_Name" HeaderText="Bank" SortExpression="Bank_Name" />
            <asp:BoundField DataField="CCY" HeaderText="Currency" SortExpression="CCY" />
            <asp:BoundField DataField="Ref_Date" DataFormatString="{0:d}" HeaderText="Contract Date" SortExpression="Ref_Date" />
            <asp:BoundField DataField="Ref_End_Date" DataFormatString="{0:d}" HeaderText="Maturity Date" SortExpression="Ref_End_Date" />
            <asp:TemplateField>
            <ItemTemplate>
            <tr>
            <td colspan="100%">
            <div id="div<%# Eval("Hedging_ID")%>" style="display: none; position: relative; left: 15px; overflow: auto">
            <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double"  BorderColor="#df5015" GridLines="None" Width="700px" HorizontalAlign="Center">
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            <RowStyle BackColor="#E1E1E1" HorizontalAlign="Center" />
            <AlternatingRowStyle BackColor="White" />
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                <Columns>
                    <asp:BoundField DataField="Invoice_Number" HeaderText="Invoice Number" SortExpression="Invoice_Number" />
                    <asp:BoundField DataField="Payment_Date" DataFormatString="{0:d}" HeaderText="Payment Date" SortExpression="Payment_Date" />
                    <asp:BoundField DataField="Payment_Amount" DataFormatString="{0:c2}" HeaderText="Payment Amount" SortExpression="Payment_Amount" />
                </Columns>
            </asp:GridView>
            </div>
            </td>
            </tr>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>


<HeaderStyle BackColor="SeaGreen" Font-Size="Medium"></HeaderStyle>


<RowStyle HorizontalAlign="Center"></RowStyle>
        </asp:GridView>

1 个答案:

答案 0 :(得分:1)

改变这样的条件

            If currency = "USD" Then
                cell.BackColor= Color.Chocolate
            Else
                cell.BackColor = Color.Red
                Label1.Text = e.Row.Cells(3).Text
            End If

可能是因为你放了“ForeColor”