简体   繁体   English

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

[英]ASP.NET GridView - Unable to get value of cell in previous row

I'm attempting to format a GridView such that when the value in a column changes from the previous value, the font color changes.我正在尝试格式化 GridView,以便当列中的值从前一个值更改时,字体颜色会更改。 I've found numerous examples of how to do this and it seems pretty straightforward, but I can't get it to work.我找到了许多如何做到这一点的例子,看起来很简单,但我无法让它工作。 I'm using the following based on another example I found changing the background color.我根据另一个示例使用以下内容,我发现更改背景颜色。

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);
    }
}

The problem is no values are returned for the current or previous row/cell, so this is always evaluates to true:问题是当前或前一行/单元格没有返回值,所以这总是评估为真:

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

Here's my GridView:这是我的 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>

What am I doing wrong?我究竟做错了什么?

I got this working as shown below.我得到了这个工作,如下所示。 Getting the values from the current and previous rows like so:从当前行和前一行中获取值,如下所示:

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)...

However, I had a lot of trouble actually getting the colors to alternate properly and ended up with something that just feels kludged to me:但是,实际上我在让颜色正确交替时遇到了很多麻烦,最终得到了一些让我感觉很困惑的东西:

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;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM