请教各位:我要利用DataGirdView批量添加,如何防止录入数据为空值或为非数字!

@信心 2011-10-20 05:18:23
利用DataGirdView批量添加数据库表,如何判断录入数据为空值为非数字!具体代码如下:
for (int i = 0; i <= complandetail.Rows.Count - 2; i++)
{
string plan = complandetail.Rows[i].Cells[0].Value.ToString();
string tsum = complandetail.Rows[i].Cells[1].Value.ToString();
string c_rate = complandetail.Rows[i].Cells[2].Value.ToString();
string p_sum = complandetail.Rows[i].Cells[3].Value.ToString();
if( plan )//录入数据验证函数,不会写了,请大家帮助了!
{
MessageBox.Show("输入内容不是数字,请检查!");
return;
}
else {

string sql="inser into pxin ()values( ) ";
com.ExecuteNonQuery();
MessageBox.Show("其他信息添加成功!";
}
...全文
116 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2011-10-23
  • 打赏
  • 举报
回复
第一种:
if (!string.IsNullOrEmpty(plan )))//判断是否为空
{
system.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
if (reg1.IsMatch(plan ))//正则判断是否数字
{}
}
第二:
int test;
if (!string.IsNullOrEmpty(plan )))//判断是否为空
{

if (int.TryParse(plan , out test))//是否数字
{
}
}
@信心 2011-10-23
  • 打赏
  • 举报
回复
遇到新问题,如果为空null  , how to do ?
hellolee213 2011-10-23
  • 打赏
  • 举报
回复
是 isnumber 。请参考。
hellolee213 2011-10-23
  • 打赏
  • 举报
回复
有个关键字,isnumeric 。你可以用这个来判断
@信心 2011-10-23
  • 打赏
  • 举报
回复
谢谢几位的指点!
sdl2005lyx 2011-10-21
  • 打赏
  • 举报
回复

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if(e.ColumnIndex==2) //控制要验证的列
{
decimal val=0.0;
if(this.textbox1.text == null && !Decimal.TryParse(this.textbox1.text ,out val) )
{
MessageBox.Show("数据为空值或为非数字!");
return;
}
}
}
轻狂小疯 2011-10-21
  • 打赏
  • 举报
回复
做判断啊 如果空值就等于什么!!
ruanwei1987 2011-10-20
  • 打赏
  • 举报
回复
OnCellValueChanged 你可以在这进行判断,这样当用户输入一个 CELL的时候就能及时提醒它做更改

也防止了,你最后一下子提出N多错误让他修改


判断是

if(this.textbox1.text != null && decimal.TryParse(this.textbox1.text ,out deTextbox1))

这样就可以了
  • 打赏
  • 举报
回复
1.你可以试图将数据转换成数字,如果转换不成功,抛出异常,那么格式不正确
2.用正则判断匹配,匹配不成功,格式不正确.

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧