如何以编程方式将空数据网格视图单元格值设置为零(0)。

问题描述:

I am creating windows application using c# 2010, here I am using data grid view, in my data grid view having 3 columns and many more rows. I am not entered all columns, alternatively I am enter the cells value.
Ex :
Slno	mts	cm
1	2	

2		3	

3		3
Here I want how set programmatically empty data grid view cell values taken zero(0).





什么我试过了:



如何以编程方式设置空数据网格视图单元格值为零(0)。



What I have tried:

How to set programmatically empty data grid view cell values taken zero(0).

绑定数据后调用下面的函数使空单元格为0



After binding the data call the below function to make the empty cells as 0

private void MakeZeroForEmptyCells()
       {
           foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               foreach (DataGridViewCell cell in row.Cells)
               {
                   string value = Convert.ToString(cell.Value);
                   cell.Value = string.IsNullOrWhiteSpace(value) ? 0 : cell.Value;
               }

           }
       }





你应该打电话给这个函数 CellValueChanged 在单元格填充空值时更新数据。



and you shall call this function on CellValueChanged to update the data when the cell is filled with empty value.