Click here to Skip to main content
15,925,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check if my "many" column in datagridview is empty.

here my code

C#
for (int i = 0; i < (gridx.Rows.Count - 1); i++)
        {
            col = gridx.Rows[i].Cells["code"].Value.ToString();
            col4 = gridx.Rows[i].Cells["many"].Value.ToString();                
        }
if (col4 == "")
        {
            MessageBox.Show("Many is empty");
            this.Focus();
            return;
        } 
else
{
//my code in here
}

but it don't show error "many is empty"

please help me.. and thanks before
Posted
Comments
Member 14887694 5-Mar-22 16:29pm    
what if you want to check for two or more columns at the same time?

Well, without debugging you can never be sure what data you are getting. As far as comparision with empty string is concerned, I would use String.IsNullOrWhiteSpace[^] Method. It checks more conditions tha just an empty string.
 
Share this answer
 
See if below code helps:

C#
bool flag = false;
foreach(DataGridViewRow row in gridx)
{
  if(Convert.ToString(row.Cells["many"].Value) == string.Empty)
     flag = true;
}

if(flag)
  MessageBox.Show("Many is empty");
else
 // your code  
 
Share this answer
 
Comments
Member 8743576 15-Jun-12 8:41am    
thank you very much sir... it works
Vani Kulkarni 18-Jun-12 1:12am    
You are welcome :)
Member 11021749 30-Aug-14 13:40pm    
thank uu
markwhite1 24-Jan-17 1:16am    
I was getting an error `foreach statement cannot operate on variables of type 'System.Windows.Forms.DataGridView' because 'System.Windows.Forms.DataGridView' does not contain a public definition for 'GetEnumerator'` So I added `foreach(DataGridViewRow row in dgvitem.Rows)` and it worked
Cell has FormattedValue property which gets formatted representation of object contained as cell's value, i.e. if cell.Value is null you'll see empty string and so on.
C#
foreach(DataGridViewRow row in yourDatagridView.Rows)
{   
    if(string.IsNullOrWhiteSpace(row.Cells["many"].FormattedValue))
    {
        MessageBox("BOOM! Many is empty",":(");
        return;
    }
}

//rest of your code here
 
Share this answer
 

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