如何根据 DataKeyName 更改 Gridview 行颜色?

How to change Gridview row color based on DataKeyName?

我希望能够根据我在 ASPX 页面中设置的数据键名更改行颜色。到目前为止,这是我尝试过的方法,但行不通。

如何引用数据键名称(这是我正在调用的 table 中的一列)并仍然根据特定文本更改颜色?该列将隐藏在网格视图中。有什么建议吗?

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem.Equals("Example Text"))
        {
            e.Row.BackColor = Color.Green;
        }
        else if (e.Row.DataItem.Equals("Other Text"))
        {
            e.Row.BackColor = Color.Orange;
        }
    }
}

DataKeyNames 是基于索引的。

string dataKeyValue = Gridview1.DataKeys[e.Row.RowIndex].Values[0].ToString();

if (dataKeyValue == "Example Text")
{
     e.Row.BackColor = Color.Green;
}