简体   繁体   中英

How to represent DB null values in grid view

i need to fetch some records from database and then bind it to a grid view.

But , in the data i am fetching from DB, there are some null values

So, Inorder to avoid type casting error , i am using a function to avoid cast error.

public static T GetValue<T>(object o)
{
    T val = default(T);

    if (o != null && o != DBNull.Value)
    {
        val = (T)o;
    }
    return val;
}

But, when i am binding to grid , it is showing as o for columns of type long . But, i need

to show as no value or nothing . Is it possible , if so please give your sugesstions

Looks like you just need to use long? instead of long as your generic parameter. I'm assuming you're currently doing something like:

long val = GetValue<long>(someDataField);

Just change that up to use the Nullable<long> type, and you'll get null back, and that should work in your grid.

long? val = GetValue<long?>(someDataField);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM