如何在C#中验证datagridview单元 [英] how to validate datagridview cell in C# a

查看:174
本文介绍了如何在C#中验证datagridview单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的datagridview之类的东西..this ...
约会日期
1 1/1/2005星期一
2 7/1/2005星期一
3 14/1/2005星期一
4 21/1/2005星期一
用户可以将日期14/1/2005更改为不少于= 7/1/2005且不得大于=大于21/1/2005
您能帮助我进行验证吗???????

感谢和问候
Lakhan

解决方案



您需要编写验证日期的逻辑,例如.

  void  dataGridView1_CellValidating( object 发​​送者,DataGridViewCellValidatingEventArgs e)
{
DateTime StartDate = " ;
DateTime EndDate = " 
DateTime enetedDate = dataGridView1.Rows [e.RowIndex] .Cells [" ];

如果(enetedDate >  = StartDate&&enetedDate< = EndDate)
{
// 您的逻辑应该在这里.
}
其他
{
 MessageBox.Show(" );
  e.Cancel =  true ;
}
} 



注意: 请先使用DataType转换,然后再使用.因为我还没有实现.

谢谢,
Imdadhusen


尝试以下链接:
如何:在Windows窗体DataGridView单元中托管控件
演练:在Windows窗体DataGridView控件中验证数据
如何:验证Windows窗体DataGridView控件中的数据


<您可以使用RangeValidator控件在其中设置minimumvalue="7/1/2005"maximumvalue="21/1/2005"
试试
RangeValidator控件 [ ^ ]


my datagridview somethink like ..this.....
sno date day
1 1/1/2005 Monday
2 7/1/2005 Monday
3 14/1/2005 Monday
4 21/1/2005 Monday
user can change date 14/1/2005 to not lessthan= 7/1/2005 and not greater= than 21/1/2005
can u help me what i have to do for this validation???????

thanks and regards
Lakhan

解决方案

Hi,

You need to write a logic for validating Date like.

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DateTime StartDate = "7/1/2005";
DateTime EndDate = "21/1/2005"
DateTime enetedDate = dataGridView1.Rows[e.RowIndex].Cells["Date"];

if( enetedDate >=  StartDate &&  enetedDate<= EndDate)
{
//Your logic should be here..
} 
else
{
 MessageBox.Show("Invalid Date!");
  e.Cancel = true;
}
}



Note: Please use DataType conversion before use. because i haven''t implemented it.

Thanks,
Imdadhusen


Try the following links:
How to: Host Controls in Windows Forms DataGridView Cells
Walkthrough: Validating Data in the Windows Forms DataGridView Control
How to: Validate Data in the Windows Forms DataGridView Control


you can use RangeValidatorcontrol where you can set minimumvalue="7/1/2005" and maximumvalue="21/1/2005"
Try
RangeValidator Control[^]


这篇关于如何在C#中验证datagridview单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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