如何在C#中编辑和编辑后设置datagridview单元格格式?

时间:2018-04-16 08:35:27

标签: c# winforms visual-studio datagridview

我的DataGridView格式在加载时是正确的。但是当我尝试编辑值并接受它(输入或制表符)时,格式不会应用。

我已添加此CellEndEdit事件,希望它会在编辑后更正格式。

private void dataGridSales_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    if (dataGridSales.CurrentCell.ColumnIndex == 2)
    {
        dataGridSales.Columns[2].DefaultCellStyle.Format = "N2";
    }
}

private void dataGridSales_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 2)
    {
        double price = Convert.ToDouble(dataGridSales.Rows[e.RowIndex].Cells[2].Value);
        dataGridSales.Columns[2].DefaultCellStyle.Format = "C2";

    }
}

private void dataGridSales_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
        dataGridSales.Columns[2].DefaultCellStyle.Format = "C2";
}

但相反它仍然没有显示正确的格式。

编辑前的单元格值:
Cell value before editing

单元格处于编辑模式:
Cell in edit mode

如何在编辑时将格式更改为N2或删除货币符号?您可以在上方看到我的CellBeginEdit事件,它会将整个列格式更改为N2。我想只更改选定的单元格。

编辑后的单元格值:
Cell value after editing

列默认单元格样式: Column Default Cell Style

dataGridSales事件代码:

    //Change Total Amount when Price Column is changed
    private void dataGridSales_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        int count = 0;
        double total = 0;
        foreach (DataGridViewRow row in dataGridSales.Rows)
        {
            total += Convert.ToDouble(row.Cells[2].Value);
        }
        lblTotAmt.Text = "Total Amount: " + total.ToString("C2");
        lblTotItem.Text = "Total Items: " + count;
    }
    //Remove item/s
    private void dataGridSales_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
    {
        int count = 0;
        double total = 0;
        foreach (DataGridViewRow row in dataGridSales.Rows)
        {
            total += Convert.ToDouble(row.Cells[2].Value);
        }
        lblTotAmt.Text = "Total Amount: " + total.ToString("C2");
        lblTotItem.Text = "Total Items: " + count;
    }
    //Price Column check
    private void dataGridSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        e.Control.KeyPress -= new KeyPressEventHandler(colPrice_KeyPress);
        //e.Control.KeyDown -= new KeyEventHandler(dataGridSales_KeyDown);
        if (dataGridSales.CurrentCell.ColumnIndex == 2)
        {
            TextBox tb = e.Control as TextBox;
            if (tb != null)
            {
                tb.KeyPress += new KeyPressEventHandler(colPrice_KeyPress);
            }
            if (e.Control is TextBox)
            {
                cprice = e.Control as TextBox;
            }
        }
    }
    //Price Column keypress only accept numbers
    private void colPrice_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
        {
            e.Handled = true;
        }
        TextBox txtDec = sender as TextBox;
        if (e.KeyChar == '.' && txtDec.Text.Contains("."))
        {
            e.Handled = true;
        }
    }

    private void dataGridSales_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (dataGridSales.CurrentCell.ColumnIndex == 2)
        {
            dataGridSales.Columns[2].DefaultCellStyle.Format = "N2";
        }
    }

    private void dataGridSales_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            double price = Convert.ToDouble(dataGridSales.Rows[e.RowIndex].Cells[2].Value);
            dataGridSales.Columns[2].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-PH");
            dataGridSales.Columns[2].DefaultCellStyle.Format = String.Format("C2");
            dataGridSales.Columns[2].ValueType = typeof(Double);
        }
    }

    //Double click on a cell to edit
    private void dataGridSales_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            dataGridSales.BeginEdit(true);
        }
    }

1 个答案:

答案 0 :(得分:1)

您的问题是您没有指定格式化程序应格式化的文化。这应该可以解决你的问题。

private void dataGridSales_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 2)
    {
        double price = Convert.ToDouble(dataGridSales.Rows[e.RowIndex].Cells[2].Value);
            dataGridSales.Columns[2].DefaultCellStyle
                                    .FormatProvider = CultureInfo.GetCultureInfo("en-US");
            dataGridSales.Columns[2].DefaultCellStyle.Format = String.Format("c");

    }
}

请注意,您应将“en-US”替换为所需货币的culture code/index

编辑:

您还应该尝试将以下代码添加到数据加载之前运行的方法:

dataGridSales.Columns[2].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US");
dataGridSales.Columns[2].DefaultCellStyle.Format = String.Format("c");
dataGridSales.Columns[2].ValueType = typeof(Double);