在DATAGRIDVIEW里有个单元,值为0.3333333,而我需要显示的是0.33,但是其实际值必须是0.3333333,请问是否有办法?也就是显示值为0.33,实际值为0.3333333。 我设置里如下格式:
dataGridView1.Columns[0].DefaultCellStyle.Format = "N2";貌似没有反应,请问是否有办法? 

解决方案 »

  1.   

    笨方法是,
    往datagriedview里面填数据的时候,处理成0.33~       
    实际用到的时候在代码里重新取一次0.3333333呗~不知道你是一行一行加的  ,还是datasource~
      

  2.   

    自定义单元格格式,你改下
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 1)//改成你的列索引
                {
                    e.CellStyle.Format = "0.####"; //N2
                }
            }
      

  3.   

    Math.Round(0.3333333,2),绑定数据列的时候这样处理一下,Math.Round(列名,2);
      

  4.   

    实在不行DataGridView.CellPainting事件里格式化再输出。
      

  5.   

    private void dataGridView1_CellPainting(object sender,
    System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
    {
        if (this.dataGridView1.Columns["ContactName"].Index ==
            e.ColumnIndex && e.RowIndex >= 0)
        {
            Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                e.CellBounds.Height - 4);        using (
                Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                backColorBrush = new SolidBrush(e.CellStyle.BackColor))
            {
                using (Pen gridLinePen = new Pen(gridBrush))
                {
                    // Erase the cell.
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);                // Draw the grid lines (only the right and bottom lines;
                    // DataGridView takes care of the others).
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                        e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom - 1);
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                        e.CellBounds.Top, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom);                // Draw the inset highlight box.
                    e.Graphics.DrawRectangle(Pens.Blue, newRect);                // Draw the text content of the cell, ignoring alignment.
                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                            Brushes.Crimson, e.CellBounds.X + 2,
                            e.CellBounds.Y + 2, StringFormat.GenericDefault);
                    }
                    e.Handled = true;
                }
            }
        }
    }
      

  6.   

    dataGridView1.Columns[0].DefaultCellStyle.Format = "N2";
    dataGridView1.Columns[0].DefaultCellStyle.ApplyStyle();
      

  7.   

    属性BoundField
    DataFormatString="{0:F2}"
      

  8.   

    你的数据是从数据库中取得还是手动输入的?
    如果数据库中取得我想在显示数据时您可以将数据处理一下
    string.format("{0:N2}",'0.3333333')
    调用时仍然使用datatable里面的数据。
    手动输入的话你可以先定义一个变量或数组来存放它用的时候直接取就行了。
      

  9.   


    难道 DefaultCellStyle 不是这样使用的,我都试过了,都没有任何反应。难道需要绑定数据才有效果?
      

  10.   

    dataGridView1.Columns[0].DefaultCellStyle.Format = "0.00";