Solved

DataGird replace  <NULL> value

Posted on 2004-10-29
267 Views
Last Modified: 2012-08-13
Hi, I have datagrid that is bound to a SQL datasource. The query retuns <NULL> values. I would like to repace all <NULL> value in my datagrid with
some other text.

How would I do it via the ItemDataBound event..


Thanks

0
Question by:xy15973
    18 Comments
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    Something like the following should work:

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
                If e.Item.Cells(0).Text Is Nothing Then
                    e.Item.Cells(0).Text = "Not defined"
                End If


            End Sub

    Best, Nauman.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    This may be an alternate way not sure if it works with e arguments or not, but I assume it will

      If IsDbNULL(e.Item.Cells(0).Text) Then
                    e.Item.Cells(0).Text = "Not defined"
      End If

    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    The way I've to do sometimes is with checking out for "" (empty string) and &nbsp; (space)...
    Following is the code for that..
    ----------------------------------------------------------------------------------------------------------------
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
         switch( e.Item.ItemType )
         {
              case ListItemType.Item:
              case ListItemType.AlternatingItem:

              if( e.Item.Cells[0].Text == "" || e.Item.Cells[0].Text == "&nbsp;" )
                    {
                         e.Item.Cells[0].Text = "0";
                    }

              break;
         }
    }
    ----------------------------------------------------------------------------------------------------------------

    -tushar
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    or alternately you could specify this in your SQL before you bind the grid.
    0
     

    Author Comment

    by:xy15973
    Thanks, do you know what the C# code would be like

    If have tried

    if (e.items.cells[0].text == DbNull) >>> error that DbNull is an class also tried DbNull.value ..cannot compare string to bool


    also tried

    if (e.items.cells[0].text == null)  >> nothing  so to if i replace null with ""..


    When i do a len on the text it is = 6 , this point to the fact that that the system does get "<NULL>"  from database


    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    if (IsDbNULL(e.Item.Cells(0).Text)) {
     e.Item.Cells(0).Text = "Not defined";
    }
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    The C# code for checking DBNull would be like following:

    if (IsDbNULL(e.Item.Cells(0).Text)) {
     e.Item.Cells(0).Text = "Not defined";
    }

    -tushar
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    ahh.. too quick Aeros..
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    hehe;)
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Correction...

    The C# code for checking DBNull would be like following:

    if (IsDbNULL(e.Item.Cells[0].Text)) {
     e.Item.Cells[0].Text = "Not defined";
    }

    -tushar
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    square brackets I should have notice that
    0
     

    Author Comment

    by:xy15973
    IsDbNull does not exsits for c#!


    0
     
    LVL 18

    Accepted Solution

    by:
    Try to compare like following & see..

     if( e.Item.Cells[0].Text == "" || e.Item.Cells[0].Text == "&nbsp;" )
                    {
                         e.Item.Cells[0].Text = "0";
                    }
    -tushar
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    hehe yay vb;)
    0
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    The C# equivalent is as follows:

    if (e.Item.Cells[0].Text != null)
    {
     e.Item.Cells[0].Text = "Not defined";
    }


    Best, Nauman.
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    You can put it right in your SQL query if you want, post the code and I'll show you how.
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    The SQL function COALESCE(a,b) will return the first non-null response.
    An example:
    SELECT
      COALESCE(Name,'Not Given'),
      COALESCE(Age,0),
      COALESCE(Date,'9/9/99')
    FROM table
    0
     
    LVL 6

    Expert Comment

    by:vardium
    I think the best way to do this is changing the SQL Select Query like nitrogenx stated.
    Another way is:
    SELECT ISNULL(Name, '') AS Name, ISNULL(Age, 0) AS Age
    FROM TABLE
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    When developing different types of ASP.NET applications I often face a requirement to let user download different types of files. They vary from logs in *.txt files or manuals/instructions in *.pdf files to charts in *.jpg files or reports in *.xls …
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now