Reputation: 16186
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?
Upvotes: 0
Views: 4693
Reputation: 1
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);
}
}
Upvotes: 0
Reputation: 53595
Using code you can set the DataGridView.DefaultCellStyle property.
You can also do this in the designer by:
Upvotes: 1