dataGridView中如何判断单元格中的值为数字还是字符类型?

jy02470229 2008-07-18 08:38:00

dataGridView中如何判断单元格中的值为数字还是字符类型?

List<double> values = new List<double>();

for (int i = 0; i < dataGridView.Columns.Count; i++)
{
for (int j = 0; j < dataGridView.Rows.Count; j++)
{
if (dataGridView.Rows[j].Cells[i].Value != DBNull.Value)
values.Add(Convert.ToDouble(dataGridView.Rows[j].Cells[i].Value));
}
}

这段代码只能对输入数值类型的值进行操作,如果dataGridView输入字符类型的,就报错!

所以想问下如何判断单元格中的值为字符类型? 高手指点下!
...全文
1204 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jy02470229 2008-07-18
  • 打赏
  • 举报
回复
dataGridView.Rows[j].Cells[i].Value 获得的本来就是字符串类型的,所以还是用try ,catch 正确!
smntbk 2008-07-18
  • 打赏
  • 举报
回复
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
if (dataGridView.Columns[i].ValueType == typeof(string)) continue;
if (dataGridView.Rows[j].Cells[i].Value != DBNull.Value)
values.Add(Convert.ToDouble(dataGridView.Rows[j].Cells[i].Value));
}
哈哈潜伏哥 2008-07-18
  • 打赏
  • 举报
回复
List <double> values = new List <double>();

for (int i = 0; i < dataGridView.Columns.Count; i++)
{
for (int j = 0; j < dataGridView.Rows.Count; j++)
{
if (dataGridView.Rows[j].Cells[i].Value != DBNull.Value)
{
try{
values.Add(Convert.ToDouble(dataGridView.Rows[j].Cells[i].Value));
}
catch
{
messagebox.show("转换错误!");
}
}
}
}
jy02470229 2008-07-18
  • 打赏
  • 举报
回复
哦 ,谢谢了!
JustLovePro 2008-07-18
  • 打赏
  • 举报
回复

if(typeof(string) == dataGridView.Rows[j].Cells[i].Value.GetType())
{
//code here
}
ming4099 2008-07-18
  • 打赏
  • 举报
回复
up
blue_summer 2008-07-18
  • 打赏
  • 举报
回复
try
Convert.ToDouble(dataGridView.Rows[j].Cells[i].Value)
catch ex
response.write("not Numeric")
end try
liumang74110 2008-07-18
  • 打赏
  • 举报
回复
up

111,120

社区成员

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

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

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