0

i have a filled GridView, what i want is to check the value in an int column called "Points" which right now is filled with Null values , some cells do have value though , that's given by the user.So it's null until it gets a value.

I want the GridView Row to change color when a Points cell gets a value, So with a quick look i can distinguish which is done and which is not!

I have managed to make an example code work for Name column, but when i tried to check for the Points column i failed :S..

here's the code:

   private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    { 
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Name")
        {
            if (e.Value != null)
            {
                try{
                string stringValue = (string)e.Value;
                stringValue = stringValue.ToLower();
                if ((stringValue.IndexOf("nikos") > -1))
                {
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Pink;
                }
                }catch (InvalidCastException  ex){
            }

            }                        
        }           
    }

how can i modify it so i can check for the null or int values in the Points column.

Thanks! :D

1 Answer 1

2
 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{ 
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Points")
    {
        if (e.Value != null)
        {
             this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Pink;
        }                      
    }           
}

Just make a check to see if the Points column is null, if its not then set the background colour

Sign up to request clarification or add additional context in comments.

1 Comment

i tried that before and thanks for your answer, but if i do this everything turns red , i suppose e.Value being an object just checks if a cell exists , so everything changes color!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.