Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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# 在GridView ASP.NET中设置DataRow的前景色(未绑定到数据源)_C#_Asp.net_Gridview_Colors_Cells - Fatal编程技术网

C# 在GridView ASP.NET中设置DataRow的前景色(未绑定到数据源)

C# 在GridView ASP.NET中设置DataRow的前景色(未绑定到数据源),c#,asp.net,gridview,colors,cells,C#,Asp.net,Gridview,Colors,Cells,我更改了第一行单元格的颜色,但我不希望第二行(分配的床位数)受到影响。我可以知道我怎么做吗 protected void bedStatsClass_OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { TableCell cell = e.Row.Cells[1];

我更改了第一行单元格的颜色,但我不希望第二行(分配的床位数)受到影响。我可以知道我怎么做吗

protected void bedStatsClass_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCell cell = e.Row.Cells[1];

            int number = int.Parse(cell.Text);

            if (number <= 10)
            {
                cell.ForeColor = Color.Red;
                cell.Style.Add("font-weight", "bold");
            }
            else if (number > 10)
            {
                cell.ForeColor = Color.ForestGreen;
                cell.Style.Add("font-weight", "bold");
            }

            //So on and so forth..
        }
    }
protectedvoid bedStatsClass\u OnRowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
TableCell cell=e.Row.Cells[1];
int number=int.Parse(cell.Text);
如果(10号)
{
cell.ForeColor=Color.ForestGreen;
单元格。样式。添加(“字体大小”、“粗体”);
}
//诸如此类。。
}
}
图示


谢谢你的帮助。谢谢。

您可以使用行索引检查行号。然后可以根据该数字为元素着色

protected void bedStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowIndex == 0)
        {
            //do stuff with the first row
        }
        if (e.Row.RowIndex % 2 == 0)
        {
            //if you want to do something to rows 1, 3, 5 etc
        }
    }
}

您可以使用行索引检查行号。然后可以根据该数字为元素着色

protected void bedStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowIndex == 0)
        {
            //do stuff with the first row
        }
        if (e.Row.RowIndex % 2 == 0)
        {
            //if you want to do something to rows 1, 3, 5 etc
        }
    }
}

有完整的数据行解决方案,即网格视图的标签,但不在数据源的数据绑定中,我们可以使用direclty而不使用OnRowDataBound。请尝试这个最好的解决方案。我们有一个例子

在aspx页面中:
有完整的数据行解决方案,即网格视图的标签,但不在数据源的数据绑定中,我们可以使用direclty而不使用OnRowDataBound。请尝试这个最好的解决方案。我们有一个例子

在aspx页面中:
您想要奇偶行为?跳过第二行的标准是什么?还是总是第一排/第三排等?不是。。只需根据条件更改第一行单元格的颜色(可用床位的数量)。至于第二排(分配的床位数量),将保持css样式设置的前景色@VDWWD第二行没有标准。。只将第二行中每个单元格的前景色设为默认颜色。您想要奇偶行为吗?跳过第二行的标准是什么?还是总是第一排/第三排等?不是。。只需根据条件更改第一行单元格的颜色(可用床位的数量)。至于第二排(分配的床位数量),将保持css样式设置的前景色@VDWWD第二行没有标准。。只需将第二行中每个单元格的前景色作为默认颜色..哦,我明白了。。请问我是否需要添加任何代码,以便计数器根据任何事件递增?要指定应将颜色添加到哪一行以及不应添加到哪一行,请执行以下操作:?虽然提供了代码,但奇偶逻辑仍然存在@不客气。我把答案简化了很多。我一时忘了有行索引。哦,我明白了。。请问我是否需要添加任何代码,以便计数器根据任何事件递增?要指定应将颜色添加到哪一行以及不应添加到哪一行,请执行以下操作:?虽然提供了代码,但奇偶逻辑仍然存在@不客气。我把答案简化了很多。我一时忘了有行索引。
This is used for the color of the grid view data rows.

   public System.Drawing.Color GetColor(string butColor)
        {
            System.Drawing.Color cr = new System.Drawing.Color();
            if (butColor == "1")
            {
                cr = System.Drawing.Color.Green;
            }
            else if (butColor == "0")
            {
                cr = System.Drawing.Color.Red;
            }
            return cr;
        }

 This is used for the status:

   protected string GetStatus(string butStatus)
        {
            if (butStatus == "1")
            {
                butStatus = "ACTIVE";
            }
            else if (butStatus == "0")
            {
                butStatus = "DEACTIVE";
            }
            return butStatus;
        }