Convert null to 0 c#




















Its quick, easy, understandable code. This has the benefit of actually changing the data to 0, instead of changing how it is viewed to 0, as others have suggested by making tweaks to the dataGrid. Either strategy has merits, just depends on what you want to do.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Convert empty cells in a data table to 0 Ask Question.

Asked 11 years, 2 months ago. Active 4 years, 3 months ago. Viewed 15k times. Improve this question. Ananth Ananth 9, 23 23 gold badges 79 79 silver badges bronze badges. Add a comment. Active Oldest Votes. Edit your cell template and set the NullValue to 0. Improve this answer. Kell Kell 3, 18 18 silver badges 19 19 bronze badges. So I dont think this will work — Ananth. You can use the?? Zebi Zebi 8, 1 1 gold badge 33 33 silver badges 42 42 bronze badges.

CodeNaked CodeNaked I like this answer. You're right, I did run into a problem using the?? ToInt32 rowData : 0; Alternatively, the?? I don't know, I kinda like your second example. I've never used the?? My second example would also do an unnecessary conversion from 0 to int , which might or might not cause you some performance trouble.

I'm not sure what type you expect from resultsDT, so you might have to put in a "0" instead of just 0 if you are expecting a string, etc. You can use extension Method. Ram Ram 35 1 1 silver badge 8 8 bronze badges. Check if it's DBNull. Value instead of null : if resultsDT. ToInt32 rowData : 0;. Dennis Traub Dennis Traub 48k 7 7 gold badges 85 85 silver badges bronze badges. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Where's DBNull in your example? That's just a simple null.

DBNull is a little different from null. Null means there is no object available. DBNull means the value is unknown. If you get a DBNull back then by definition the value is not currently known to you. How can you convert that into a decimal? You'll need to decide on a default value yourself and assign it. Add a comment. Active Oldest Votes. How do you expect to cast null to anything? It's null, nothing, nada Improve this answer. SetFreeByTruth 7 7 silver badges 23 23 bronze badges.

Matthew Scharley Matthew Scharley k 51 51 gold badges silver badges bronze badges. But in answer; not really Marc Gravell Marc Gravell k gold badges silver badges bronze badges.

Show d. ToString ;. Nikki Nikki 5, 1 1 gold badge 26 26 silver badges 22 22 bronze badges. Have you considered using a nullable-decimal for your return value?

The code might look something like this Null return null; else return decimal. Parse value. Bevan Bevan Length - 1] ;.



0コメント

  • 1000 / 1000