Justin
Justin

Reputation: 972

System.DBNull Error on formatting DataGridView C#

I'm new to C# so I have been following some guides and adapting it to fit my application. Here is one of the ones I followed from Microsoft.

I have used the same code as what they have suggested and I get an error.

enter image description here

My code simply looks at the data in the DataGridView and highlights the line with red if the condition is met. This is my code:

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

        //Set the row definition
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        //If we are on the validating column
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "SelectedUID")
        {
            //check criteria
            string stringValue = (string)e.Value;
            if ((stringValue.IndexOf("JL") > -1))
            {
                Debug.WriteLine("The Format Value " + e.Value);
                //format the row colour
                row.DefaultCellStyle.ForeColor = Color.White;
                row.DefaultCellStyle.BackColor = Color.Red;
            }
            else
            {
                Debug.WriteLine("The Value " + e.Value);
            }
        }

    }

What is it that I have got wrong with the conversion of the object? I have tried many other solutions. Additional to this I tried to test:

if (e.value != null) 

but this did not even read a null value in there so I'm not sure what was being returned because I checked the length and it was 0.

Upvotes: 0

Views: 1458

Answers (4)

v2v2
v2v2

Reputation: 110

try this : string stringValue =Convert.ToString(e.Value);

or you can use Nullable keyword in datatype.

Upvotes: 0

user586399
user586399

Reputation:

First check if the value is DBNull:

        if (Convert.IsDBNull(e.Value) {

        } else {
           string stringValue = (string)e.Value;
           if ((stringValue.IndexOf("JL") > -1))
           {
               Debug.WriteLine("The Format Value " + e.Value);
               //format the row colour
               row.DefaultCellStyle.ForeColor = Color.White;
               row.DefaultCellStyle.BackColor = Color.Red;
           }
           else
           {
              Debug.WriteLine("The Value " + e.Value);
           }
        }

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

DBNull.Value is of type DBNull and cannot be cast to a string. Instead, use Convert.ToString to handle this case.

Demo

Also, for what it's worth, DBNull.Value is not the same as null. See this answer for details.

Upvotes: 3

Hinek
Hinek

Reputation: 9729

The value of this field in the database is NULL, you can't cast DBNull to a string. Try:

string stringValue = e.Value as string;

... if cou want the stringValue to be null, when the database value is DBNull, or:

string stringValue = e.Value == DBNull.Value ? string.Empty : (string)e.Value;

... if you want the stringValue to be empty (""), when the database value is DBNull.

Upvotes: 0

Related Questions