C # Foreach Loop - Continue Release

I have a problem with the continue statement in my C # Foreach loop.

I want it to check if there is an empty cell in the datagridview, and if so, skip printing the value and continue to check the next cell.

Help evaluate very much.

Here is the code:

foreach (DataGridViewRow row in this.dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Size.IsEmpty) { continue; } MessageBox.Show(cell.Value.ToString()); } } 
+6
c # foreach continue datagridview
source share
4 answers

Well, you are currently checking to see if the cell size is zero. In a grid, each cell in a column has the same width, and each cell in a row has the same height (usually anyway).

You want to check based on cell value. For example:

 if (cell.Value == null || cell.Value.Equals("")) { continue; } 

Change this for any other representations of "empty" values ​​that interest you. If there are a lot of them, you can write a specific method for it and call it on the check:

 if (IsEmptyValue(cell.Value)) { continue; } 
+16
source share

You do not need to use the continue keyword, you can just do this:

 foreach (DataGridViewRow row in this.dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (!cell.Size.IsEmpty) MessageBox.Show(cell.Value.ToString()); // note the ! operator } } 

In addition, you check if the cell size is free. Is this really what you want to do?

What kind of mistakes do you get?

+4
source share

Should I check to see if the cell value is empty, not size?

 if(String.IsNullOrEmpty(cell.Value.ToString())) continue; 
+2
source share

I want to read only cells [1] data ... olny

 foreach (DataGridViewRow row in this.dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells[1]) { if (cell[1].Value == null || cell.Value.Equals("")) { continue; } MessageBox.Show(cell[1].Value.ToString()); } } 
+1
source share

All Articles