1

我在 C# 网格视图中使用一些代码,我让它根据值标记不同的颜色。我还想通过更改红色背景的前景色使其更易于阅读。可能是一个愚蠢的问题,但我怎样才能在代码中实现这一点,它将标记背景色为红色和前色为白色?我已经尝试了几种方法,但在这里都没有成功。它适用于任何一个或但我想在这里使用两者。

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        System.Data.DataRow row = ((System.Data.DataRowView)e.Row.DataItem).Row;
        if (row["Qty To Sell"].ToString() == "1")
            e.Row.BackColor = System.Drawing.Color.LightSalmon;
        else if (row["Qty To Sell"].ToString() == "3")
            e.Row.BackColor = System.Drawing.Color.LightSalmon;
        else if (row["Qty To Sell"].ToString() == "2")
            e.Row.BackColor = System.Drawing.Color.LightSalmon;
        else if (row["Qty To Sell"].ToString() == "0")
            e.Row.BackColor = System.Drawing.Color.Red;
4

1 回答 1

0

我使用标签属性来做到这一点。

定义你的号码参数:

int two = 2;
int four = 4;

找到你的标签:

Label my_label = (Label)row.FindControl("your_label");

将该标签值转换为整数:

int lbl_value = Convert.ToInt32(my_label.Text);

然后在 if 语句中更改标签属性:

if ( lbl_value == two)
    { 
      my_label.BackColor = System.Drawing.Color.White; 
      my_label.Forecolor = System.Drawing.Color.Red;  
     }

等等。我喜欢只更改标签属性而不是整行。

于 2013-09-23T18:26:11.027 回答