GridView的条件单元格格式化没有在asp.net中发生

时间:2013-08-28 07:10:26

标签: asp.net gridview

我一直困在一个看似非常简单的问题,但我不知道出了什么问题。我是Asp的新手,因此不了解调试基础知识。

我正在尝试根据结果列中的值更改单元格文本的颜色。我附上了代码供您参考。请告诉我什么是错的,不要反映出来。

                                                            

                <AlternatingRowStyle CssClass="alt" />

                <Columns>

                    <asp:BoundField DataField="TestCaseID" HeaderText="TestCaseID" SortExpression="TestCaseID" />
                    <asp:BoundField DataField="Iteration" HeaderText="Iteration" SortExpression="Iteration" />
                    <asp:BoundField DataField="StartTime" HeaderText="StartTime" SortExpression="StartTime" />
                    <asp:BoundField DataField="EndTime" HeaderText="EndTime" SortExpression="EndTime" />
                    <asp:BoundField DataField="Result" HeaderText="Result" SortExpression="Result" />
                    <asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
                </Columns>


                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>
            <asp:EntityDataSource ID="EntityDataSource1" runat="server"
                ConnectionString="name=Test_Manager_DatabaseEntities"
                DefaultContainerName="Test_Manager_DatabaseEntities"
                EnableFlattening="False" EntitySetName="TestExecutionDetails"
                Where="it.[TestExecutionID] = @ExecutionID ">
                <WhereParameters>
                    <asp:QueryStringParameter Name="ExecutionID" QueryStringField="ExecutionID" Type="Int32" />
                </WhereParameters>
            </asp:EntityDataSource>
             <asp:QueryExtender ID="ctlMyQueryExtender" runat="server" TargetControlID="EntityDataSource1"> <asp:SearchExpression DataFields="Result" SearchType="StartsWith"> <asp:ControlParameter ControlID="TextBoxName" /></asp:SearchExpression> </asp:QueryExtender>
        </ContentTemplate>
    </asp:UpdatePanel>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ABCD
{
    public partial class TestExecutionResults : System.Web.UI.Page
    {
        private int TestExecID;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.CacheControl = "no-cache";
            if (!Page.IsPostBack)

                using (Test_Manager_DatabaseEntities entities = new Test_Manager_DatabaseEntities())
                {
                    Console.WriteLine(Request.QueryString["ExecutionID"]);
                    TestExecID = Convert.ToInt32(Request.QueryString["ExecutionID"]);
                }
}

protected void Timer2_Tick(object sender, EventArgs e)
        {

            GridView1.DataBind();

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[4].Text == "Pass")
                {
                    e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
                }

                else if (e.Row.Cells[4].Text == "Fail")
                {
                    e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;

                }
            }

        }
    }
}

我已尝试过stackoverflow中的所有解决方案,有人可以帮我调试这个。我添加了断点并检查。它看起来像if条件失败

if (e.Row.RowType == DataControlRowType.DataRow)

,因为它没有继续进入这个条件,而是回到调用函数。我也尝试删除计时器。

1 个答案:

答案 0 :(得分:0)

这是一个简单的错误,

if (e.Row.Cells[4].Text.Trim().ToLower() == "pass")
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
            }