Iterate Each Cell Of DataGridView

user2932408

I want to iterate each cell of my DataGridView and if the value = 0 or 0.00 I want to change it to a - I have the below code, but I am getting an error of

Possible unintended reference comparison

What would be the proper way to achieve what I am after here?

foreach (DataGridViewRow rw in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in rw.Cells)
    {
       if (rw.Cells[0].Value == "0")
       {
          rw.Cells[0].Value = "-";
       }
    }
}
mu88

According to the MSDN documentation, DataGridViewCell.Value returns an object. Now you'd like to compare this with your string "0".
When trying to compare object and string, you will compare their references, but not their values. Basically you're just checking whether they point to the same object.
If your cell ALWAYS contains objects of type string, you could simply do the following:

foreach (DataGridViewRow rw in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in rw.Cells)
    {
        var currentValue = (string)rw.Cells[0].Value;
        if (currentValue == "0")
        {
            currentValue = "-";
        }
    }
}

A more safe approach:

foreach (DataGridViewRow rw in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in rw.Cells)
    {
        var currentValue = rw.Cells[0].Value as string;
        if (currentValue != null && currentValue == "0")
        {
            currentValue = "-";
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Making a list for each cell in a datagridview

From Dev

Iterate over each cell in a row RadGrid in C#

From Dev

Change of the edited cell in a datagridview cell

From Dev

How can i change color of particular row in datagridview, where a cell value is first timestamp of each day?

From Dev

DataGridView Numeric Only Cell?

From Dev

Cell Formatting In DataGridView On DataBindingComplete

From Dev

Casting datagridview cell as float

From Dev

Multiline text in DataGridView Cell

From Dev

Custom DataGridView Cell Painting

From Dev

Datagridview cell with background image

From Dev

DataGridView Cell Value Changing

From Dev

Apply Formula Cell to a DataGridview

From Dev

How to delete a datagridview cell?

From Dev

DataGridView Numeric Only Cell?

From Dev

comparison of datagridview cell values

From Dev

Datagridview coloring cell

From Dev

DataGridView Cell Value Changing

From Dev

Colour a cell in datagridview

From Dev

Updating A DataGridView Cell Incrementally

From Dev

Datagridview Header cell

From Dev

DataGridView double underline Cell

From Dev

Hiding a value in a datagridview cell

From Dev

How to iterate through each cell in a worksheet range and unlock them based on a Regex match

From Dev

need to Iterate through the cells in the array, and set the value of each cell to be the product of its coordinates

From Dev

Matlab: iterate 2d cell array and map each row to variables

From Dev

Efficiently iterate in cell

From Dev

DataGridView System.InvalidOperationException Cell is not in a DataGridView

From Dev

Looping each row in datagridview

From Dev

Looping each row in datagridview