DataGridView decimal value formatting: kosher way
I need to display decimal values in DataGridView
if (dgvClients.SelectedRows.Count != 0)
{
DataGridViewRow row = dgvClients.SelectedRows[0];
row.Cells["NetAccountValue"].Value = info.DecimalValue开发者_运维知识库;
}
I need to format value up to given digits after decimal separator.
The problem is that cell Value
property stores reference to decimal in my case. When displayed decimal.ToString() is called and default decimal.ToString() produces unformatted string
with lots of digits.
One way is to create string and use String.FormatString() to achieve desired formatting and feed it Value instead of decimal.
Is there some other way?
Using code you can set the DataGridView.DefaultCellStyle property.
You can also do this in the designer by:
- Right clicking your DGV and select Edit Columns. The Edit Columns dialog appears.
- Select your column on the left side of the dialog and click the DefaultCellStyle property field to bring up the CellStyle Builder.
- In the Builder dialog click Format which brings up the Format String Dialog.
- Make your selections in this dialog (select Numeric type, which allows specifying number of decimals to display) and save your changes.
i found this solution simply.i have searched along two days,found it finally. -your db column type must be Money or number,double,single. -Update datagridview event "CellFormatting" as below code.
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);
}
}
精彩评论