简体   繁体   English

如何在datagridview c#.net中显示空白而不是0

[英]how to show blank instead of 0 in datagridview c#.net

I am working on a window application Visual Studio 2010 and SQL Server 2008. My code is working fine.我正在开发一个窗口应用程序 Visual Studio 2010 和 SQL Server 2008。我的代码运行良好。 I need only one thing that if any value is 0.000 in database, it should be shown blank in DataGridView .我只需要一件事,如果数据库中的任何值为0.000 ,它应该在DataGridView显示为空白。 Please, give me solution.请给我解决方案。 Thanks in advance.提前致谢。 Code is given below:代码如下:

sda = new SqlDataAdapter("select Item, Cast (Val1 AS DECIMAL (10,3)), Cast (Val2 AS DECIMAL (10,2)), Cast (Val3 AS DECIMAL (10,2)), Cast (Val4 AS DECIMAL (10,3)), sno from Opstk where Vno ='" + txtVno.Text + "'",  con);
        DataTable dt = new DataTable();
        sdaq.Fill(dt);

        dataGridView1.Rows.Clear();
        foreach (DataRow item in dt.Rows)
        {
            int n = dataGridView1.Rows.Add();
            dataGridView1.Rows[n].Cells[0].Value = item["Item"].ToString();
            dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
            dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
            dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
            dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();
            dataGridView1.Rows[n].Cells[5].Value = item[5].ToString();

        }

All you need to do is set a Format for either the DataGridView's global DefaultCellStyle , maybe like this:您需要做的就是为DataGridView's全局DefaultCellStyle设置一个Format ,可能是这样的:

dataGridView1.DefaultCellStyle.Format = "###.##";

or for the one of a certain column或某一列

dataGridView1.Columns[yourColumn].DefaultCellStyle.Format = "###.##";

Note however that you are adding the values not as numbers but as strings .但是请注意,您添加的值不是作为数字而是作为字符串 This is not necessarily wrong, but it will prevent doing calculations and other things.这不一定是错误的,但它会阻止进行计算和其他事情。 It will also prevent a numeric format like above to work.它还会阻止像上面这样的数字格式工作。

Either add them as numbers (recommended) or simply use the format you want right in the ToString function:要么将它们添加为数字(推荐),要么直接在ToString函数中使用您想要的格式:

dataGridView1.Rows[n].Cells[1].Value = item[1].ToString("###.##");

But again: Now the numbers are lost and you would have to waste time if you ever want to use them..但是再说一遍:现在数字丢失了,如果您想使用它们,您将不得不浪费时间。

Of course you may want to pick some other numeric format string ..当然,您可能想选择其他一些数字格式字符串..

If the value of the grid cell is 0, but you don't want it to display 0, you can set the value of the cell to DBNull.Value如果网格单元格的值为0,但又不想显示为0,可以将单元格的值设置为DBNull.Value

if( item[1] == 0 ) 
{
    dataGridView1.Rows[n].Cells[1].Value = DBNull.Value;
}

Later, if you need the 0 value for a calculation, you can reverse稍后,如果您需要 0 值进行计算,则可以反转

int myValue = 0;
if (dataGridView1.Rows[n].Cells[1].Value != DBNull.Value) 
    { myValue = dataGridView1.Rows[n].Cells[1].Value; }

You can try this:你可以试试这个:
double value = Convert.ToDouble(item["Item"]); dataGridView1.Rows[n].Cells[0].Value = value == 0 ? "" : value.ToString(); But this is dirty, you should call datagridView1.DataSource = yourDataTable;但这很脏,你应该调用datagridView1.DataSource = yourDataTable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM