从datagridview C#中检索数字值

编程入门 行业动态 更新时间:2024-10-22 08:15:18
本文介绍了从datagridview C#中检索数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从datagridview检索数值。表中的值和变量(weeklyTotal)的数据类型均为整数。我也试图将其转换为整数。我在整个网站上浏览了类似的问题,但是没有一种解决方案有用。我收到的错误消息是当转换数字时,该值必须小于无穷大。我已多次返回表以确保没有无效值。

I am trying to retrieve a numerical value from datagridview. The data type for both the value in the table, and the variable (weeklyTotal), are integer. Also I am trying to cast it into integer. I looked through the whole website for similar problems, and yet none of the solutions were helpful. The error message that I am getting is "when casting form a number, the value must be less than infinity". I went back to my table more than one time to make sure that I don’t have invalid value.

这是运行时错误,IDE始终指向此行

it's a runtime error and the IDE always point at this line

weeklyTotal + =(int)dataGridView1.Rows [i] .Cells [1] .Value;

for (int i = 0; i < this.dataGridView1.Rows.Count; i++) { sdate = dataGridView1.Rows[i].Cells[2].Value.ToString(); ddate = dataGridView1.Rows[i].Cells[3].Value.ToString(); if ((weekFirstDay <= Convert.ToInt32(sdate) && Convert.ToInt32(sdate) <= (weekFirstDay + 7))||(weekFirstDay <= `Convert.ToInt32(ddate) &&` Convert.ToInt32(ddate) <= (weekFirstDay + 7))) { dataGridView1.Rows[i].Selected = true; dataGridView1.Rows[i].Visible = true; weeklyTotal += (int)dataGridView1.Rows[i].Cells[1].Value; //weeklyTotalString = dataGridView1.Rows[i].Cells[1].Value.ToString(); //weeklyTotal += Convert.ToInt32(weeklyTotalString); } else

推荐答案

好吧,这是因为

(int)dataGridView1.Rows[i].Cells[1].Value

不会将您的单元格值转换为int类型。您只是将字符串说为嘿编译器,请将其视为int类型,但它仍然是下面的字符串类型。像以前一样使用 Convert.ToInt32 方法。

won't cast your cell value to the int type. You're just taking the string saying "hey compiler, please treat it as an int type" but it still will be a string type underneath. Use the Convert.ToInt32 method like you did earlier.

更多推荐

从datagridview C#中检索数字值

本文发布于:2023-10-27 03:19:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!