dgv填充值为0的空单元格 [英] dgv fill empty cell with value 0

查看:76
本文介绍了dgv填充值为0的空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一些小问题,(我无法解决:(



我有导入excel文件的dgv,它和一列包含一些数字,我需要的是该列中的每个单元格都要填充值(CAN NOT BE EMPTY)并且在按钮单击时我试图以编程方式填充它但它没有工作



这是按钮点击的代码



foreach(dgImport中的DataGridViewRow行。行)

{

if(row.Cells [Expense]。Value == null)

{

row.Cells [Expense]。值=0;

}



}

dgImport.Update();

dgImport.Refresh();

Hello,

I have some small problem, (which I can not solve :(

I have dgv with imported excel file in, it, and one columns holds some numbers, what I need is each cell in that column to be filled with value (CAN NOT BE EMPTY) and on button click I was trying to programatically filled it but it's not working

this is the code on button click

foreach (DataGridViewRow row in dgImport.Rows)
{
if (row.Cells["Expense"].Value == null)
{
row.Cells["Expense"].Value = "0";
}

}
dgImport.Update();
dgImport.Refresh();

推荐答案

我建​​议你阅读这些文章:

使用ADO.NET处理Excel文件 [ ^ ]

如何使用ADO.NET使用Visual Basic检索和修改Excel工作簿中的记录.NET [ ^ ]



为了避免在需要数值时出现空值或空字符串,请使用 IIF 功能:

I would recommend you to read these articles:
Using ADO.NET to work with Excel files[^]
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET[^]

To avoid nulls or empty strings when numeric value is needed, please use IIF function:
SELECT IIF(NumericField1<>'',NumericField1, 0) AS NumericField1, TextField2, OtherField3
FROM [Sheet1



代表OP ...

On behalf of OP...
Quote:

对不起打扰你们,我找到了解决方案,这很有效,谢谢大家,谢谢

sorry for bothering you all , I found solution, this works, thank you all, thank you

foreach (DataGridViewRow row in dgImport.Rows)
{
    if (string.IsNullOrEmpty(row.Cells["Potrosnja"].Value.ToString()))
    {
        row.Cells["Expense"].Value = 0;
    }   
}
dgImport.Refresh();


这篇关于dgv填充值为0的空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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