DataGridView C#编辑模式编辑后的值格式化 [英] DataGridView C# Edit Mode Value Formatting After Edit

查看:633
本文介绍了DataGridView C#编辑模式编辑后的值格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#Winform项目中有一个datagridview。此datagridview用于将值插入数据库。每列都匹配相应的数据库列。



其中一列是DateTime列。我想验证这个日期,以确保它是正确的格式。理想情况下,您将离开单元格,并将其转换为我选择的日期时间格式。



我已经使用一系列的事件来尝试创建这个功能,但遇到问题。我所保留的问题是,当事件触发时,该值尚未存储在单元格中。因此,当我做这样的事情,例如:

  private void Grid_Modify_CellEndEdit(object sender,DataGridViewCellEventArgs e)
{
if(Grid_Modify.Columns [e.ColumnIndex] .Name ==DateTime)
{
Grid_Modify [e.ColumnIndex,e.RowIndex] .Value =
转换。 ToDateTime(Grid_Modify [e.ColumnIndex,e.RowIndex] .Value).ToString(YYYY-MM-DD hh:mm:ss);
}
}

问题是,它转换为datetime的值是原始单元格值 - 而不是我刚刚输入的新值。我假设这是因为事件在更新单元格值之前触发。



问题是:当我更改其值时,最好的格式化日期是什么?



我尝试过的其他事件是:CellLeave,CellValueChanged,CellValidated和CellEndEdit。



注意:CellValueChanged事件是实际上可以获取新单元格值的唯一事件,但是当我更改事件处理程序中的值时,它被捕获在无限循环中。

解决方案

最简单的方法是返回到CellValueChanged事件处理程序,并使用全局变量来避免无限循环:

  private bool _inCellValueChanged = false; 

然后,在CellValueChanged中:

  if(!_inCellValueChanged&& Grid_Modify.Columns [e.ColumnIndex] .Name ==DateTime)
{
_inCellValueChanged = true;
Grid_Modify [e.ColumnIndex,e.RowIndex] .Value = Convert.ToDateTime(Grid_Modify [e.ColumnIndex,e.RowIndex] .Value).ToString(YYYY-MM-DD hh:mm:ss) ;
_inCellValueChanged = false;
}


I have a datagridview in a C# Winform project. This datagridview is used to insert values into a database. Each column matches up to a corresponding db column.

One of the columns is a DateTime column. I want to 'validate' this date to ensure it is in the proper format. Ideally, you would leave the cell and it would convert it to a date time format that I choose.

I have used a series of events to attempt creating this functionality but have been having problems. The problem that I keep having is that when the event fires, the value has not been stored in the cell yet. Therefore, when I do something like this for example:

private void Grid_Modify_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (Grid_Modify.Columns[e.ColumnIndex].Name == "DateTime")
    {
        Grid_Modify[e.ColumnIndex, e.RowIndex].Value = 
           Convert.ToDateTime(Grid_Modify[e.ColumnIndex, e.RowIndex].Value).ToString("YYYY-MM-DD hh:mm:ss");
    }
}

Problem is, the value it is converting to a datetime is the original cell value -- not the new value I just entered. I assume this is because the event is firing before the cell value is being updated.

Question is: What is the best way of formatting the date when I change its value?

The other events I have tried are: CellLeave, CellValueChanged, CellValidated, and CellEndEdit.

NOTE: CellValueChanged event was the only event that actually could get the new cell value, however, it gets caught in an infinite loop when I change the value within the eventhandler.

解决方案

The simplest would be to go back to CellValueChanged event handler and use a global variable to avoid your infinite loop:

 private bool _inCellValueChanged = false;

Then, in CellValueChanged:

if (!_inCellValueChanged && Grid_Modify.Columns[e.ColumnIndex].Name == "DateTime") 
{ 
    _inCellValueChanged = true;        
    Grid_Modify[e.ColumnIndex, e.RowIndex].Value = Convert.ToDateTime(Grid_Modify[e.ColumnIndex, e.RowIndex].Value).ToString("YYYY-MM-DD hh:mm:ss");
    _inCellValueChanged = false; 
} 

这篇关于DataGridView C#编辑模式编辑后的值格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆