在DataGridView里有一列是数字字段,如果该值是零,就不显示,或显示为空,如何设置?
0.00;0都不让它显示或为空。

解决方案 »

  1.   

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
            { 
                for (int i = 0; i < dataGridView1.RowCount;i++) 
                { 
                    for(int j=1;j <4;j++) 
                        if (dataGridView1.Rows[i].Cells[j].Value = 0) 
                        { 
                            dataGridView1.Rows[i].Cells[j].Value= ""; 
                        } 
                } 
                
            } 
    结贴吧
      

  2.   

    有错误,这样才对
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
            { 
                for (int i = 0; i < dataGridView1.RowCount;i++) 
                { 
                    for(int j=1;j <4;j++) 
                        if (dataGridView1.Rows[i].Cells[j].Value == "0" ||dataGridView1.Rows[i].Cells[j].Value == "0.00")
                        { 
                            dataGridView1.Rows[i].Cells[j].Value= ""; 
                        } 
                } 
                
            } 
      

  3.   

    一定要这么麻烦?有没有简单一点的方法?
    在VFP里format属性设为'Z'就行了,C#有没有这样的属性?
      

  4.   

    3楼的已经很简单啦,还有一种就是用datatable赋值,你的gridview就绑定这个datatable,这样你想怎么赋值都可以
      

  5.   


    if (Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value) == 0) 

      dataGridView1.Rows[i].Cells[j].Value= ""; 
    }