views:

9159

answers:

5

Hello! I have a datagridview made up of multiple rows and columns. I want to iterate through each row and check the contents of a specific column. If that column contains the word "NO", I want to change the forecolor of the entire row to Red. Here is an attempt at some code so far but It's certainly not working, starting to wonder If I need to iterate over every cell?

CODE:

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
        {
            if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
            {
                dgvr.DefaultCellStyle.ForeColor = Color.Red;
            }
        }
+10  A: 

hook up OnRowDataBound event then do stuff

ASPX (Grid):

    <asp:.... OnRowDataBound="RowDataBound"..../>

Code Behind:

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1)
        {
            return;
        }

        if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){
             e.Row.BackColor=Color.Red;   
        }
    }

FOR WinForms:

hook the **DataBindingComplete** event and do stuff in it:

     private void dataGridView1_DataBindingComplete(object sender, 
                       DataGridViewBindingCompleteEventArgs e)
    {
        if (e.ListChangedType != ListChangedType.ItemDeleted)
        {
            DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
            red.BackColor=Color.Red;

            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Cells["FollowedUp"].Value.ToString()
                       .ToUpper().Contains("NO"))
                {
                    r.DefaultCellStyle = red;
                }
            }
        }
    }
TheVillageIdiot
Sorry this is just a straight WinForms app.........
Goober
oops! everything is so web and I'm myself deep in web project since 3 months that it seems normal that every question is about asp.net
TheVillageIdiot
+1  A: 

Is it possible there are spaces or some other character as part of the cell value? If so try using the Contains method rather than straight equality.

if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
William Edmondson
A: 

This is the solution for Winforms:

private void HighlightRows()
{
    DataGridViewCellStyle GreenStyle = null;

    if (this.dgridv.DataSource != null)
    {
        RedCellStyle = new DataGridViewCellStyle();
        RedCellStyle.BackColor = Color.Red;

        for (Int32 i = 0; i < this.dgridv.Rows.Count; i++)
        {
            if (((DataTable)this.dgridv.DataSource).Rows[i]["col_name"].ToString().ToUpper() == "NO")
            {
                this.dgridv.Rows[i].DefaultCellStyle = RedCellStyle;
                continue;
            }
        }
    }
}
Rashmi Pandit
A: 

This code works fine for me:


foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if ((string)row.Cells["property_name"].Value == UNKNOWN_PROPERTY_NAME)
    {
     row.DefaultCellStyle.BackColor = Color.LightSalmon;
     row.DefaultCellStyle.SelectionBackColor = Color.Salmon;
    }
}

Other than casting as a string rather than calling ToString I dont really see any difference so it could be a case sensitivity bug. Try using:

dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO"
+1  A: 
public void ColourChange()
    {
        DataGridViewCellStyle RedCellStyle = null;
        RedCellStyle = new DataGridViewCellStyle();
        RedCellStyle.ForeColor = Color.Red;
        DataGridViewCellStyle GreenCellStyle = null;
        GreenCellStyle = new DataGridViewCellStyle();
        GreenCellStyle.ForeColor = Color.Green;


        foreach (DataGridViewRow dgvr in dataGridView1.Rows)
        {
            if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
            {
                dgvr.DefaultCellStyle = RedCellStyle;
            }
            if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("Yes"))
            {
                dgvr.DefaultCellStyle = GreenCellStyle;
            }
        }
    }
Goober
You don't gain anything by doing [Type x = null; x = new Type();]. You should be merging those two lines into [Type x = new Type();]
Mark Rushakoff