如何在DataGrid列中仅验证月份和年份 [英] How to validate for only month and year in datagrid column

查看:97
本文介绍了如何在DataGrid列中仅验证月份和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

any1能否告诉我如何在datagridview(Windows应用程序)中仅针对月份和年份验证文本框列.

提前谢谢.

问候,
Shridevi

Hi All,

Can any1 tell me how to validate the textbox column for only month and year in datagridview (windows application).

Thanks in advance.

Regards,
Shridevi

推荐答案

从问题中可以看出,要求是验证DataGridView TextBoxColumn 中的条目,以便仅month 和<输入c3>.为此,我认为DataGridView CellValidating 事件可以按如下方式使用:
As seen from the question it seems that the requirement is to validate the entry in the TextBoxColumn of DataGridView such that only month and year is entered. For this purpose I think the the CellValidating event of DataGridView can be used as follows:
dataGridView1.CellValidating += (s, args) => {
    //If the column index is not the index of the desired column then return
    if (args.ColumnIndex != 2)
        return;
    if (!Regex.IsMatch((string)args.FormattedValue,@"\d{2}/\d{4}", RegexOptions.CultureInvariant)){
        MessageBox.Show("Please enter month and year as 05/2012");
        args.Cancel=true;
    }

};


在上面的示例中,使用了MM/yyyy格式.您可以使用适当的正则表达式模式检查其他格式,包括有效范围.


In the above example the MM/yyyy format is used. You can check any other format including the valid ranges with appropriate regular expression pattern.


简单技巧:将每个日期的天数替换为1,现在您可以进行验证了
在DataGridView中 DataBindingComplete [ ^ ]事件:
Simple trick: replace day for each date with 1 and now you can validate
In a DataGridView DataBindingComplete[^] event:
foreach (DataGridViewRow i in dataGridView1.Rows)
{
    String s = i.Cells[Index].ToString;
    DateTime d = DateTime.Parse(s);
    d.Day = 1
    i.Cells[Index].Value = d.ToString();
}



我没有C#编译器,所以如果我在上面的代码中出错,请原谅.



I don''t have C# compilator, so excuse me if i made an error in above code.


这篇关于如何在DataGrid列中仅验证月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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