【发布时间】:2018-09-25 22:22:32
【问题描述】:
我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";
}
但它仍然没有显示正确的格式。
如何将格式更改为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);
}
}
【问题讨论】:
-
这是winform还是wpf?
-
@Alander 在 winform 中。
标签: c# winforms visual-studio datagridview