Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET GridView-无法获取前一行中单元格的值_C#_Asp.net - Fatal编程技术网

C# ASP.NET GridView-无法获取前一行中单元格的值

C# ASP.NET GridView-无法获取前一行中单元格的值,c#,asp.net,C#,Asp.net,我正在尝试设置GridView的格式,以便当列中的值与前一个值相比发生变化时,字体颜色也会发生变化。我已经找到了很多这样做的例子,看起来很简单,但我无法让它工作。我使用下面的例子是基于我发现的另一个改变背景颜色的例子 protected void gvEscalationLinks_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow)

我正在尝试设置GridView的格式,以便当列中的值与前一个值相比发生变化时,字体颜色也会发生变化。我已经找到了很多这样做的例子,看起来很简单,但我无法让它工作。我使用下面的例子是基于我发现的另一个改变背景颜色的例子

protected void gvEscalationLinks_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Debug.WriteLine("e.Row.DataItemIndex: " + e.Row.DataItemIndex);

        if (e.Row.DataItemIndex == 0)
        {
            e.Row.Cells[1].BackColor = Color.AliceBlue;
            return;
        }

        GridViewRow prevRow = gvEscalationLinks.Rows[e.Row.RowIndex - 1];

        GridViewRow thisRow = e.Row;
        e.Row.Cells[1].BackColor = (thisRow.Cells[1].Text == prevRow.Cells[1].Text) ? Color.AliceBlue : Color.White;

        // no values are ever produced:
        Debug.WriteLine("prevRow.Cells[1].Text: " + prevRow.Cells[1].Text);
        Debug.WriteLine("thisRow.Cells[1].Text: " + thisRow.Cells[1].Text);
    }
}
问题是当前或上一行/单元格未返回任何值,因此此值始终为true:

(thisRow.Cells[1].Text == prevRow.Cells[1].Text) ? Color.AliceBlue : Color.White;
以下是我的GridView:

<asp:GridView ID="gvEscalationLinks" runat="server" OnRowDataBound="gvEscalationLinks_RowDataBound" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Category">
            <ItemTemplate> 
                <asp:Label ID="lblCategory" runat="server" Text='<%# Eval("escCategory") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SubCategory">
            <ItemTemplate> 
                <asp:Label ID="lblSubCategory" runat="server" Text='<%# Eval("escSubCategory") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Link">
            <ItemTemplate>
                <asp:HyperLink ID="lnkURL" NavigateUrl='<%# Eval("docurl") %>' runat="server"><%# Eval("linkname") %></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Supporting Manager">    
            <ItemTemplate>
                <asp:Label ID="lblManager" runat="server" Text='<%# Eval("supportMgr") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


我做错了什么?

我的工作原理如下所示。从当前行和上一行获取值,如下所示:

GridViewRow thisRow = e.Row;
Label currentValue = thisRow.Cells[2].FindControl("lblSubCategory") as Label;

GridViewRow prevRow = gvEscalationLinks.Rows[e.Row.RowIndex - 1];
Label previousValue = prevRow.Cells[2].FindControl("lblSubCategory") as Label;

if (previousValue.Text != currentValue.Text)...
然而,实际上,我在让颜色正确交替方面遇到了很多麻烦,结果是我觉得有些困惑:

protected void gvEscalationLinks_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridViewRow thisRow = e.Row;
        Label currentValue = thisRow.Cells[2].FindControl("lblSubCategory") as Label;

        if (e.Row.DataItemIndex == 0)
        {
            e.Row.Cells[1].BackColor = Color.AliceBlue;
            Session["color"] = "AliceBlue";
            return;
        }

        GridViewRow prevRow = gvEscalationLinks.Rows[e.Row.RowIndex - 1];
        Label previousValue = prevRow.Cells[2].FindControl("lblSubCategory") as Label;

        if (previousValue.Text != currentValue.Text)
        {
            if ( Session["color"].ToString() == "AliceBlue" )
            {
                e.Row.Cells[1].BackColor = Color.White;
                Session["color"] = "White";
            }
            else
            {
                e.Row.Cells[1].BackColor = Color.AliceBlue;
                Session["color"] = "AliceBlue";
            }
        }
        else
        {
            if (Session["color"].ToString() == "AliceBlue")
            {
                e.Row.Cells[1].BackColor = Color.AliceBlue;
            }
            else
            {
                e.Row.Cells[1].BackColor = Color.White;
            }
        }
    }
}

使用调试器,在相关行设置断点,并检查prevRow.Cells[1]中的值。Text@nicomp-不确定你的建议是什么;我已经在用Debug.WriteLine为每一行吐出prevRow.Cells[1]文本。断点有什么不同的功能?