如何在DataGridViewColumn中对齐小数位

时间:2016-03-16 18:43:09

标签: c# winforms

我有一个客户不希望小数点后跟零。他希望1.2显示为1.2和0.0125显示为0.0125,没有尾随零。但他也希望小数点在列中排成一行。对于我如何做到这一点,或者即使可以做到这一点,我也不是最模糊的。

更新

使用评论和建议......

我的自定义控件:

public sealed partial class BufferedDataGridView : DataGridView
{
    public BufferedDataGridView()
    {
        this.DoubleBuffered = true;

        InitializeComponent();
    }
}

我的Windows窗体:

public partial class Form1 : Form
{
    private readonly BufferedDataGridView _dgv = new BufferedDataGridView();

    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        IList<NumberList> numbers = LoadSampleData();

        Font font = new Font("Consolas", 8, FontStyle.Regular);

        _dgv.DefaultCellStyle.Font = font;

        _dgv.CellFormatting += Dgv_CellFormatting;

        _dgv.CellDoubleClick += Dgv_CellDoubleClick;

        Controls.Add(_dgv);

        _dgv.Dock = DockStyle.Fill;

        bindingSource1.DataSource = numbers;
        _dgv.DataSource = bindingSource1;

        base.OnLoad(e);
    }

    private void Dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCell cell =
            _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];

        string stringValue = cell.Value.ToString();

        decimal value;
        decimal.TryParse(stringValue, out value);

        Debug.WriteLine("decimal value: " + value);
    }

    private static IList<NumberList> LoadSampleData()
    {
        NumberList number1 = new NumberList {Number = 100.25M.ToString(CultureInfo.CurrentCulture)};
        NumberList number2 = new NumberList {Number = 3.128M.ToString(CultureInfo.CurrentCulture)};
        NumberList number3 = new NumberList {Number = 10.2M.ToString(CultureInfo.CurrentCulture)};
        NumberList number4 = new NumberList {Number = .1257M.ToString(CultureInfo.CurrentCulture)};
        NumberList number5 = new NumberList {Number = 1000M.ToString(CultureInfo.CurrentCulture)};
        NumberList number6 = new NumberList {Number = .0128M.ToString(CultureInfo.CurrentCulture)};

        IList<NumberList> numbers = new List<NumberList>();

        numbers.Add(number1);
        numbers.Add(number2);
        numbers.Add(number3);
        numbers.Add(number4);
        numbers.Add(number5);
        numbers.Add(number6);

        return numbers;
    }

    private void Dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value != null)
        {
            DataGridViewCell cell =
                _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];

            string originalCell = e.Value.ToString();

            string reformattedCell = FormatCell(originalCell);

            cell.Value = reformattedCell;
        }
    }


    private static string FormatCell(string originalCell)
    {
        if (originalCell.Contains("."))
        {
            string wholeNumber = originalCell.Split('.')[0];

            string decimalNumber = originalCell.Split('.')[1];

            return wholeNumber.PadLeft(7) + "." + decimalNumber;
        }
        return originalCell.PadLeft(7);
    }
}

我的号码列表类:

public class NumberList
{
    public string Number { get; set; }
}

我必须创建Class属性类型字符串而不是decimal,然后解析出值。有没有人有建议所以我可以让数字列表类使用十进制类型而不是字符串?

0 个答案:

没有答案