问题
Basically I have a datagridview which displays data from a datatable. Each cell in the datatable is a decimal with roughly 10 decimal points, how can I use the datagridview to display this data to 2 decimal points?
I have tried this (what I found in other questions):
for (int i = 0; i < this.dgvDynamics1.Columns.Count; i++)
this.dgvDynamics1.Columns[i].DefaultCellStyle.Format = "N2";
but it doesn't work
If you need any more info let me know
Thanks in advance
回答1:
Try this:
this.dgvDynamics1.Columns.Items[i].DefaultCellStyle.Format = "0.00##";
this.dgvDynamics1.Columns.Items[i].ValueType = GetType(Double)
回答2:
try this
private void dgvList_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 6 || e.ColumnIndex == 8)
{
e.CellStyle.Format = "N2";
}
}
回答3:
This is an old post, but for the record, this should work:
for (int i = 0; i < this.dgvDynamics1.Columns.Count; i++)
this.dgvDynamics1.Columns[i].DefaultCellStyle.Format = "0.00";
来源:https://stackoverflow.com/questions/25281427/formatting-datagridview-cell-to-2-decimal-places