设置 SQLCE 中日期的格式以在 DataGridView 中显示
我有一个 DataGridView 通过 BindSource 绑定到 .sdf 数据库中的表。日期列显示日期,如“d/M/yyyy HH:mm:ss”。例如:“1971 年 2 月 27 日 00:00:00”。
我想让它在其位置仅显示“27/02/1971”。我尝试应用 DataGridViewCellStyle {format=dd/MM/yyyy} 但没有任何反应,事件与其他预构建格式。
另一方面,有一个带有 MasketTextBox 的表单,其输入带有“dd/MM/yyyy”掩码,该掩码绑定到同一列,并在显示之前使用 Parse 和 Format 事件处理程序并将其发送到数据库。
Binding dataNascimentoBinding = new Binding("Text", this.source, "Nascimento", true);
dataNascimentoBinding.Format += new ConvertEventHandler(Util.formatDateConvertEventHandler);
dataNascimentoBinding.Parse += new ConvertEventHandler(Util.parseDateConvertEventHandler);
this.dataNascimentoTxt.DataBindings.Add(dataNascimentoBinding);
public static string convertDateString2DateString(string dateString, string inputFormat, string outputFormat )
{
DateTime date = DateTime.ParseExact(dateString, inputFormat, DateTimeFormatInfo.InvariantInfo);
return String.Format("{0:" + outputFormat + "}", date);
}
public static void formatDateConvertEventHandler(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(string)) return;
if (e.Value.GetType() != typeof(string)) return;
String dateString = (string)e.Value;
e.Value = convertDateString2DateString(dateString, "d/M/yyyy HH:mm:ss", "dd/MM/yyyy");
}
public static void parseDateConvertEventHandler(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(string)) return;
if (e.Value.GetType() != typeof(string)) return;
string value = (string)e.Value;
try
{
e.Value = DateTime.ParseExact(value, "dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo);
}
catch
{
return;
}
}
就像您在代码中看到的那样,来自 SQL 的 Date 将是一个 DateTime 值,就像它的列一样,但我的 eventHandler 正在接收一个字符串。同样,解析的结果日期应该是日期时间,但它也是字符串。
我对处理这个日期时间列感到困惑。
I have a DataGridView bound to a table from a .sdf database through a BindSource. The date column display dates like "d/M/yyyy HH:mm:ss". e.: "27/2/1971 00:00:00".
I want to make it display just "27/02/1971" in its place. I tried to apply DataGridViewCellStyle {format=dd/MM/yyyy} but nothing happens, event with other pre-built formats.
On the the other side, there's a Form with a MasketTextBox with a "dd/MM/yyyy" mask to its input that is bound to the same column and uses a Parse and a Format event handler before display and send it to the db.
Binding dataNascimentoBinding = new Binding("Text", this.source, "Nascimento", true);
dataNascimentoBinding.Format += new ConvertEventHandler(Util.formatDateConvertEventHandler);
dataNascimentoBinding.Parse += new ConvertEventHandler(Util.parseDateConvertEventHandler);
this.dataNascimentoTxt.DataBindings.Add(dataNascimentoBinding);
public static string convertDateString2DateString(string dateString, string inputFormat, string outputFormat )
{
DateTime date = DateTime.ParseExact(dateString, inputFormat, DateTimeFormatInfo.InvariantInfo);
return String.Format("{0:" + outputFormat + "}", date);
}
public static void formatDateConvertEventHandler(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(string)) return;
if (e.Value.GetType() != typeof(string)) return;
String dateString = (string)e.Value;
e.Value = convertDateString2DateString(dateString, "d/M/yyyy HH:mm:ss", "dd/MM/yyyy");
}
public static void parseDateConvertEventHandler(object sender, ConvertEventArgs e)
{
if (e.DesiredType != typeof(string)) return;
if (e.Value.GetType() != typeof(string)) return;
string value = (string)e.Value;
try
{
e.Value = DateTime.ParseExact(value, "dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo);
}
catch
{
return;
}
}
Like you can see by the code it was expexted that Date coming from SQL would be a DateTime value as is its column, but my eventHandler is receiving a string instead. Likewise, the result date for parse should be a datetime but its a string also.
I'm puzzled dealing with this datetime column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了。必须在“数据集”视图中将列类型设置为您希望在应用程序中使用的类型。因此,如果您只是使用 Parse 和 Format 方法将其更改为 DateTime,即使用户更改了 PC 区域设置,您也是安全的。
完成此操作后,DataGridView 中的格式开始工作。
Found the problem. The column type must be set in the DataSet view to the type you want in your application. So if you just change it to DateTime with a Parse and Format method you are safe even if the user changes the PC locale.
After doing it the format in DataGridView starts to work.