如果在c#中其单元格包含0值时,如何将一些文本设置为datagridview单元格 [英] How to set some text to datagridview cell when its cell contains 0 value in c#

查看:93
本文介绍了如果在c#中其单元格包含0值时,如何将一些文本设置为datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview及其来自sql数据库的数据..

我的datagridview一些单元格填充0(零)值。

我想设置一个NULL文本在datagridview中填充0值的位置。



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.

//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

推荐答案

然后你可以直接从数据库中获取NULL值是SQL语句

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





i我不确定哪个列显示0表示前检查第一个co lumn





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

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


<itemtemplate>
 <asp:label id="lblStatus" runat="server" text="<%# SetZeroToNull(Eval("value").ToString()) %>" xmlns:asp="#unknown" />
</itemtemplate>





现在在代码后面编写SetZeroToNull()方法就好了。




now write SetZeroToNull() method in code behind like.

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







或者你可以写任何你想要的东西




or you can write any thing you want


这篇关于如果在c#中其单元格包含0值时,如何将一些文本设置为datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆