Change GridView row color based on condition

前端 未结 7 1001
时光说笑
时光说笑 2020-12-01 14:04

I want to change a particular row color of gridview based on some condition, I am using ASP.NET with c#. Thank you.

相关标签:
7条回答
  • 2020-12-01 14:11
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("style", "cursor:help;");
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
        { 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");
                e.Row.BackColor = Color.FromName("#E56E94");                
            }           
        }
        else
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");
                e.Row.BackColor = Color.FromName("gray");                
            }           
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:15

    Create GridView1_RowDataBound event for your GridView.

    //Check if it is not header or footer row
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Check your condition here
        If(Condition True)
        {
            e.Row.BackColor = Drawing.Color.Red // This will make row back color red
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:16

    This method modifies both the back color (to dark red) and the text (to white) if a specific string ("TextToMatch") occurs in one of the columns:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[8].Text.Equals("TextToMatch"))
        {
            e.Row.BackColor = System.Drawing.Color.DarkRed;
            e.Row.ForeColor = System.Drawing.Color.White;
        }
    }
    

    Or another way to write it:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[8].Text.Equals("TextToMatch"))
        {
            e.Row.Attributes.CssStyle.Value = "background-color: DarkRed; color: White";
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:23
    \\loop throgh all rows of the grid view  
    
    if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value1")
    {
       GridView1.Rows[i - 1].ForeColor = Color.Black;
    }
    else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value2")
    {
       GridView1.Rows[i - 1].ForeColor = Color.Blue;
    }
    else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value3")
    {
       GridView1.Rows[i - 1].ForeColor = Color.Red;
    }
    else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value4")
    {
       GridView1.Rows[i - 1].ForeColor = Color.Green;
    }
    
    0 讨论(0)
  • 2020-12-01 14:24
    protected void DrugGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    
    {
        // To check condition on integer value
        if (Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Dosage")) == 50)
        {
          e.Row.BackColor = System.Drawing.Color.Cyan;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:26
     protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_Code = (Label)e.Row.FindControl("lblCode");
                if (lbl_Code.Text == "1")
                {
                    e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#f2d9d9");
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题