Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to display NULL in a datagrid

Posted on 2005-04-25
10
Medium Priority
?
255 Views
Last Modified: 2010-04-23
Hi there,

How can I display a NULL in a datagrid?

the cells i want to display "NULL" in, have a datatype of double.

It's when i have a value of -99 that i want to display NULL in the datagrid

Thanks for the help
0
Comment
Question by:se402036se
  • 6
  • 3
10 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13857683
before binding datatable to datagrid , find all occurences of -99 and change them to System.DBNyll.Value. AFter that bind dt to the datagrid and by default it will show "null"
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13857687
another way fo doing it


For Each rows In tmp_dataview.Table.Rows
   rows("FIeldName") = ManageNull(rows("FieldName"))
Next
DataGrid1.DataSource = tmp_dataview

    Private Function ManageNull(ByVal aValue As System.Object) As System.Object
        If aValue = -99  Then
            Return "null"
        Else
            Return aValue
        End If
    End Function
0
 

Author Comment

by:se402036se
ID: 13857893
good, but the type of the object in my cell is Double and when i try to say:

rows("FIeldName") = "NULL"
then i get an error.

Is there a way i can not change the cell vlaue but on the front end i.e. what the user sees, display NULL.

So the datasource of the cell = -99 but what is being displayed on the screen is "NULL"?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:iboutchkine
ID: 13857933
yes in this case you have to change -99 to null in your database or datatable. On a screen will be displayed   -  [null]
0
 
LVL 41

Expert Comment

by:graye
ID: 13858096
I'd recommend that you use a DataGridColumnStyle (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridcolumnstylememberstopic.asp) to "decorate" your DataGrid.

The DataGridColumnStyle will allow you to set what text you want to appear in the grid when the data is NULL.  It doesn't alter the underlying database at all, and doesn't require any extra programming.

There is a fairly good example at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridcolumnstyleclasstopic.asp 
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13858140
graye - he has the reverse task, not to show something when it is null, but show null when he has -99.  I assume that you meant NullText property of the ColumnStyle
0
 

Author Comment

by:se402036se
ID: 13858560
the values are already stored as  NULL in my database.

It's when i take the data from the database into my recordset. In my recordset i repesent system.null as -99.

So now the NULL values that where in my database are now stores in a recordset as -99 (of type double)

I then say:
myGrid..DataSource = myRecordSet

Where myRecordSet has a load of -99 values.

How now can i display "NULL" instead of -99 in my dataGrid without changing the value -99 before i set the datasouse?

Sorry for dragging this out, i really appreciate the help
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 13858675
Don't  repesent system.null as -99.  Leave it as null and you will get null in your datagrid. If you still need to do it, then loop through your myRecordSet
 and change -99 to Null before
myGrid.DataSource = myRecordSet
0
 

Author Comment

by:se402036se
ID: 13859043
i know i does sounds stupid what i am trying to do.

it's just that the system i'm working on represents NULL as -99. So it takes NULLs from the database changes them to -99 then changes them back to NULL defore saving them back into the database. Dumb i know.

So am i correct in saying that once the datasource of my DataGrid is, say, an array of -99 values then there is no way of displaying NULL in the dataGrid unless i:
Loop through my array as change all -99 to NULLs before i set the datasource?

the equivelent of
.NullText = <something i want to display instead of NULL>
I want
.-99Text = <something i want to display instead of -99>
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 400 total points
ID: 13859261
It is a way if the datatype of this column is a string. You have double datatype and the word null will bomb out, but you can display 0 if you want. If that fits your needs than use the approach I showed you in the code above (only change "null" to 0)


For Each rows In tmp_dataview.Table.Rows
   rows("FIeldName") = ManageNull(rows("FieldName"))
Next
DataGrid1.DataSource = tmp_dataview

    Private Function ManageNull(ByVal aValue As System.Object) As System.Object
        If aValue = -99  Then
            Return 0
        Else
            Return aValue
        End If
    End Function
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question