2012-07-17 73 views
0

我已经使用下面的答案在我的Visual Basic 2010 DGV中显示小数值。Datagridview中缺少小数值

使用代码可以设置DataGridView.DefaultCellStyle属性。

您还可以通过做到这一点的设计师

1.Right clicking your DGV and select Edit Columns. The Edit Columns dialog appears. 
2.Select your column on the left side of the dialog and click the DefaultCellStyle property field to bring up the CellStyle Builder. 
3.In the Builder dialog click Format which brings up the Format String Dialog. 
4.Make your selections in this dialog (select Numeric type, which allows specifying number of decimals to display) and save your changes. 

我的问题是,当我通过一个文本框,绑定到DGV输入新的值,该DGV显示小数位,但不我输入的实际价值。例如:我输入20.25,点击保存按钮。在DGV中,20.25的值表示。只要我关闭程序并再次打开,DGV中的值显示为20.00,而不是20.25。

我该如何解决这个问题?

+1

你如何坚持用户在单元格中输入的值? – 2012-07-17 11:06:24

回答

0

我发现这个解决方案simply.i已经搜索了两天,最终找到它。 - 您的db列类型必须为金钱或数量,双倍,单一。更新dataGridview事件“CellFormatting”,如下面的代码。

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (this.dataGridView2.Columns[e.ColumnIndex].Name == "Aidat") 
     { 
      string deger=(string)e.Value; 
      deger = String.Format("{0:0.00}", deger); 
     } 
    }