gpt4 book ai didi

c# - 从 datagridview C# 中检索数字值

转载 作者:行者123 更新时间:2023-12-02 08:37:48 26 4
gpt4 key购买 nike

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

这是一个运行时错误,IDE 总是指向这一行

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 类型:

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

你只是把字符串说“嘿编译器,请将它视为 int 类型”,但它仍然是一个字符串类型。像之前一样使用 Convert.ToInt32 方法。

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

关于c# - 从 datagridview C# 中检索数字值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com