0

I'm reading all the cells in a DataGridView, but on each of them I want to compare the first 3 elements of the cell as a string, and turn it into a blank cell if they match.

So far I've tried this

 for (int z = 0; z < dataGridView7.Rows.Count; z++)
 {
     for (int i = 0; i < dataGridView7.Columns.Count; i++)
     {
          string caixa = dataGridView7[z, i].Value.ToString(); //problem right here
          string caixaselecta = caixa.Substring(0, 3);

          if (caixaselecta == "0 *")
          {
              dataGridView7[z, i].Value = "";
          }
    }
}

The for cycles are correctly done, and the matches too. I think the problem is identifying the current cell being analyzed, and turn it into a string.

I've tried similar ways to reach the current cell, but either I get a error, or the string caixa isn't correctly defined.

Any hints?

RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41
ng80092b
  • 621
  • 1
  • 9
  • 24

1 Answers1

0

You are specifying the index in wrong order. DataGridView indexer is defined as

public DataGridViewCell this[
    int columnIndex,
    int rowIndex
] { get; set; }

-Referenced from MSDN documentation

so you need to swap the z and i in indexer.

for (int z = 0; z < dataGridView7.Rows.Count; z++)
{
    for (int i = 0; i < dataGridView7.Columns.Count; i++)
    {
        string caixa = dataGridView7[i, z].Value.ToString();
        ....
        ....
        ....
    }
}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62