Check whether an Object is empty or null

11,065

You can get the actual edited Value by using the .EditedFormattedValue

if (string.IsNullOrWhiteSpace(gvProductBatch.Rows[i].Cells["txtSellQty"].EditedFormattedValue.ToString())
{
  //Do something
}
Share:
11,065
Prathap
Author by

Prathap

Updated on June 04, 2022

Comments

  • Prathap
    Prathap almost 2 years

    I have a DataGridView and added a column called SellQty and a Checkbox at index 0. User has to enter int value when he selects a checkbox. If not I am showing a message to enter the value. Now the problem is I am getting the value from the SellQty cell and storing it in an object and checking whether it is null.

    object SellQty = gvProductBatch.Rows[i].Cells["txtSellQty"].Value;
    if(SellQty!=null)
        // do something
    
    else 
        // ..Show message.
    

    This works fine. But the problem is when the user enters a value and deletes it, the value stored in it is {} i.e. empty. I would like to know how to check an object is empty. I have googled for the same but didn't find answer to handle empty object. All results were for if object is null.

  • Prathap
    Prathap about 11 years
    I am using 3.5.Anywaz I have used NullorEmpty and it works great.Thanks mate.Can you just explain a bit about this line of code.
  • spajce
    spajce about 11 years
    heres the msdn reference will explain to you. welcome :)
  • Prathap
    Prathap about 11 years
    Gone through it.Thanks.I have changed the validation and calculations in my entire module.It works great. I didn't handle this exception when user enters a white space in whole module.Now it works great
  • spajce
    spajce about 11 years
    you use like this way. if (gvProductBatch.Rows[i].Cells["txtSellQty"].EditedFormattedV‌​alue.ToString().Trim‌​().Length == 0) { //Do something }