Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中编辑时和编辑后如何设置datagridview单元格格式?_C#_Winforms_Visual Studio_Datagridview - Fatal编程技术网

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

在C#中编辑时和编辑后如何设置datagridview单元格格式?,c#,winforms,visual-studio,datagridview,C#,Winforms,Visual Studio,Datagridview,myDataGridView的格式在加载时是正确的。但是,当我尝试编辑值并接受它(输入或制表符)时,格式不会应用 我已经添加了这个CellEndEdit事件,希望它在编辑后能够更正格式 private void dataGridSales_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (dataGridSales.CurrentCell.ColumnIndex == 2) {

my
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";

    }
}

但是它仍然没有显示正确的格式

编辑前的单元格值:

编辑模式下的单元格:

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

编辑后的单元格值:

列默认单元格样式:

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);
        }
    }

您的问题是没有指定格式化程序应在其上格式化的区域性。这应该能解决你的问题

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”替换为所需货币的名称

编辑:

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

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

这是winform还是wpf?@Alander在winform中。我已经测试过了,应该可以用了,你能分享
dataGridSales
的所有事件吗?编辑:能否尝试将此代码应用于Form_Load方法或任何等效方法(在将数据加载到网格视图之前)<代码>dataGridSales.Columns[2]。DefaultCellStyle.FormatProvider=CultureInfo.GetCultureInfo(“en-US”);dataGridSales.Columns[2].DefaultCellStyle.Format=String.Format(“c”);dataGridSales.Columns[2].ValueType=typeof(双精度)我已将defaultstyle列设置为上图,还发布了所有dataGridSales事件。编辑后仍然没有应用该格式。我已经将第二组代码添加到我的表单加载中,它现在正在工作。Thanks@Jepher很高兴这有帮助。我的荣幸。
dataGridSales.Columns[2].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-US");
dataGridSales.Columns[2].DefaultCellStyle.Format = String.Format("c");
dataGridSales.Columns[2].ValueType = typeof(Double);