Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview and its data from sql database..
My datagridview some cells filled with 0(zero) value.
I want to set a NULL text at where the 0 value is filled in the datagridview.

C#
//I'm using this code but not working
 private void dataGridView4_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        for (int i = 0; i < dataGridView4.Rows.Count; i++)
        {
            for (int j = 7; j < dataGridView4.Rows[i].Cells.Count; j++)
            {
                if (Convert.ToInt32(dataGridView4.Rows[i].Cells[j].Value) == 0)
                {
                    dataGridView4.Rows[i].Cells[j].Value = "Null";
                }

            }
        }

    }

//this query is what i used to get the data as 0 when there is no column in that table

select l.admission_number,l.student_class ,l.student_name,hindi,maths,social,l.telugu,l.english ,
'' as english_1,'' as english_2,l.science, '' as ns,'' as ps  
from lkg_to_seventh_marks as l
union all 
select e.admission_number ,e.student_class ,
e.student_name ,hindi,maths,social,e.telugu,'' as english,e.english_1 ,e.english_2,'' as science,e.ns,e.ps 
from eighth_to_ninth_marks as e
Posted
Updated 27-Oct-14 23:36pm
v2
Comments
abdussalam143 28-Oct-14 3:36am    
are you using asp.net of is it on windows form
n shiva Ram 28-Oct-14 5:33am    
It is win-form

then you can directly get the NULL value from database is SQL Statement
SQL
CASE WHEN YOURCOLUMN=0 THEN NULL ELSE YOURCOLUMN END


i am not sure which column is showing 0 for ex check the first column

SQL
select CASE WHEN l.admission_number=0 THEN NULL ELSE l.admission_number END ADMISSION_NUMBER,l.student_class ,l.student_name,hindi,maths,social,l.telugu,l.english ,
'' as english_1,'' as english_2,l.science, '' as ns,'' as ps  
from lkg_to_seventh_marks as l
union all 
select e.admission_number ,e.student_class ,
e.student_name ,hindi,maths,social,e.telugu,'' as english,e.english_1 ,e.english_2,'' as science,e.ns,e.ps 
from eighth_to_ninth_marks as e
 
Share this answer
 
v2
Comments
n shiva Ram 28-Oct-14 5:34am    
where should i Use this line
Basmeh Awad 28-Oct-14 6:37am    
you said "I have a datagridview and its data from sql database.."
so for extracting from sql database you need a sql statement in that statement the column which you want null in the place of 0 or else post your Select statement i'll try to help
n shiva Ram 28-Oct-14 6:49am    
Have a look on my question please,i HAVE ADDED MY QUERY
Basmeh Awad 28-Oct-14 9:03am    
Try now
ASP.NET
<itemtemplate>
 <asp:label id="lblStatus" runat="server" text="<%# SetZeroToNull(Eval("value").ToString()) %>" xmlns:asp="#unknown" />
</itemtemplate>


now write SetZeroToNull() method in code behind like.

C#
private string SetZeroToNull(string value)
{
    if (value == "0")
    {
        return null;
    }
    else
    {
        return value;
    }
}



or you can write any thing you want
 
Share this answer
 
Comments
n shiva Ram 28-Oct-14 5:42am    
I'm working on win-apps,not asp.net

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900