c# dataGridView如何获取某一单元格数据

来源:百度知道 编辑:UC知道 时间:2024/05/29 16:02:34
dataGridView已绑定表Score,显示ID和Score两个字段的值
我想获取dataGridView显示的Score字段的值(共10个记录)赋给10元素double类型数组array中,我用的代码是
for (i = 0; i < dataGridView1.Rows.Count ;i++ )
{
array[i] = (double )dataGridView1.Rows[i].Cells[1].Value ;
}
为啥提示强制转换无效,值必须是一个小于无限大的数呢?
应该怎样写代码?
MSDN上的代码也看过,网上也搜过代码,基本上都是用引用dataGridView1.Rows[行号].Cells[列号].Value ,为啥转换成double就不行了....

用array[i] = (double )dataGridView1.Rows[i].Cells[2].Value.ToString ()后又说无法将string转换为double = =

如果去掉double强制转换又提示无法将string隐式转换为double,因为array数组是double类型的所以应该还是要转换一下吧!

array[i] = dataGridView1.Rows[i].Cells[1].Value as double;
不行的话
array[i] = dataGridView1.Rows[i].Cells[1].Value.ToString() as double;

发现楼上的朋友们没提到利用Convert.来强制转换,在这里我就补充下!
array[i] = Convert.ToDouble(dataGridView1.Rows[i].Cells[1].Value);

Convert可以用于好多种类型的强制转换,你自己点下就知道了!这么好用的怎么没发现人说- -~

LZ试一下

array[i] = double.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString ())

不知道行不行

应该是转换类型有错误 你把那个前面的转换类型的去掉试试

类型转换错误。
.ToString()后再转换!

这个在MSDN上有代码示例,我就不献丑了。

你打开MSDN一看就知道了。。。