简体   繁体   中英

How to change datagridview colors based on value contained within cells

Hi all I would like to change cell index based on certain value like shown below ,I have seen many articles in here, but in ASP.NET this is a windows app how can I archive this thanks with a windows desktop app. Please Note the column that I want indexes changed is created dynamically.Thanks Dynamic column code creation

private void button3_Click(object sender, EventArgs e)
        {
              DataTable table = new DataTable();
                adap.Fill(table);
                dataGridView1.DataSource = table;
       table.Columns.Add("RESULTS").Expression = "Iif(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))),'GOOD''BAD'))

 foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[7].Value.ToString() == "BAD")
                    row.Cells[7].Style.ForeColor = Color.Red;
                //row.Cells["RESULTS"].Style.ForeColor = Color.Red;
            }

}

As you are adding a new column to the datatable table , you need to bind the table to the datagridview (refer this : how to bind datatable to datagridview in c# ) and then try to change the color.

Try this

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red; //change the color to what you need
} 

UPDATE : To iterate through the DataGridView and check for cell contents in a specific column, you need something like

foreach(DataGridViewRow row in dataGridView1.Rows)
{
    if (row.Cells[7].Value.ToString() == "BAD")
        row.Cells[7].Style.ForeColor = Color.Red;
        //row.Cells["RESULTS"].Style.ForeColor = Color.Red;
}

You need to place this piece of code inside an event that is triggered or function that is called after the DataGridView is populated with data.

在此处输入图片说明

Here is some sample code demonstrating the desired result

ASPX:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="true">
</asp:GridView>

Code behind:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList al = new ArrayList();
                al.Add("open"); al.Add("close"); al.Add("other"); al.Add("open"); al.Add("other"); al.Add("close"); al.Add("open");
                this.GridView1.DataSource = al;
                this.GridView1.DataBind();
            }
        }

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "open")
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                }
                else if (e.Row.Cells[0].Text == "close")
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Black;
                }
                else
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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