寻找未来&无效日期并在按钮单击时将其空白 [英] Find the future & invalid dates and blank it on a button click
本文介绍了寻找未来&无效日期并在按钮单击时将其空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在DateGridView中有Date字段。单击按钮,我将选择特定列以查找未来和无效(例如:00/11 / 2000,11 / 00 / 2001,11 / 12/0000)。
并将所有这些空白。能告诉我最好和最短的过程吗?
Hi,
I have Date fields in DateGridView. On a button click, i will select the particular column to find the FUTURE and INVALID (Ex: 00/11/2000, 11/00/2001, 11/12/0000).
And blank all those. Can you please let me know the best and short process?
推荐答案
这个过程是逐个遍历行并检查该列中的值
您可以使用 TryParse [ ^ ]或 TryParseExact [ ^ ]检查日期是否是否有效。对于日期,我推荐后者(TryParseExact),以便文化和格式不会引起问题。
The process is to go through the rows one by one and check the value in that column
You can use TryParse[^] or TryParseExact[^] to check whether the date is valid or not. For dates I recommend the latter (TryParseExact) so that Culture and format does not cause issues.
const int colWithDate = 0;
for (var i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[0].Value != null)
{
DateTime checkDate;
if (!DateTime.TryParseExact(GridView1.Rows[i].Cells[colWithDate].Value.ToString(), "d/m/yyyy", new CultureInfo("en-GB"),DateTimeStyles.None, out checkDate))
GridView1.Rows[i].Cells[0].Value = "";
}
}
这篇关于寻找未来&无效日期并在按钮单击时将其空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文