Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am checking which cells in a column are null or in blank and then retrieving the row Index of the empty cells found.

I use this code to find the index:
C#
StringBuilder sb = new StringBuilder();
foreach(DataGridViewRow row in mainDGV.Rows)
{      
      if(row.Cells[0].Value.ToString() == null)
      {
              sb.Append(Properties.Settings.Default.userDB[row.Index].ToString(); //this gets the username from a stringCollection using the rowIndex.
      }
}
MessageBox.Show(sb.ToString());


But this only shows me the first username that has not registered in the DGV. Even using the stringBuilder, only the first username appends to it.

How can I get all the users that have not registered (have not entered a value in the datagridview).

Thanks in Advance - CCB
Posted
Comments
sreeyush sudhakaran 6-Aug-15 0:59am    
Sorry your question is not clear , Can you please make it more clear?
ChrisCreateBoss 6-Aug-15 15:39pm    
Ok, the rows of my datagridview are created depending of the count of strings in a StringCollection allocated in the Properties.Settings.Default. I am trying to check every cell in my datagridview and see which ones are null or blank. If there are blank cells I want to get the rowIndex of each of them and based on the rowIndex, display the matching username that is allocated in the stringCollection. The problem is that, if I get three blank cells, I just get the first matching username and not all the matching ones. Now u got it?
Did you debug and see what is happening?
ChrisCreateBoss 6-Aug-15 15:40pm    
Yes I debug but I do not get any exceptions or errors in the code.

1 solution

C#
StringBuilder sb = new StringBuilder();
foreach(DataGridViewRow row in mainDGV.Rows)
{
//changed line
//Reason:.Tostring() method will throw  System.NullReferenceException when it
//finds a empty cell.
      if(!(row.Cells[0].Value is string))//or if( Convert.ToString(row.Cells[0].Value)=="")
      {
              sb.Append(Properties.Settings.Default.userDB[row.Index].ToString(); //this gets the username from a stringCollection using the rowIndex.
      }
}
MessageBox.Show(sb.ToString());
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900