新手求助:dataGridView2.Rows[i].Cells[j].Value.ToString()得到的值怎么转化为int

时光泡烂了过往 2016-04-01 11:27:48
这个语句得到的值是string类型的,因为要用这个值来判断大小,所以想转化为int型的,但是用了Int32.Parse和Convert.ToInt32,都提示:输入字符串的格式不正确,求大神们帮忙看看

  private void button2_Click_1(object sender, EventArgs e)
{
string value1,value2;
value1 = dataGridView1.Rows[0].Cells[16].Value.ToString();
int a =Int32.Parse(value1);
textBox2.Text = value1;
// for (int i = 0; i <= this.dataGridView2.RowCount; i++) {
// for (int j = 2; j <= 7; j += 2)
if (i < this.dataGridView1.Rows.Count)
{
if (j <= 7)
{
{
string num1 = (string)dataGridView2.Rows[i].Cells[j].Value.ToString();
int b=Int32.Parse(num1);
//int b = Convert.ToInt32(num1);
string num2 = (string)dataGridView2.Rows[i].Cells[j + 1].Value.ToString();
int c=Int32.Parse(num2);
// int c = Convert.ToInt32(num2);
if (a <= c && a >= b)
{
value2 = (string)dataGridView2.Rows[i].Cells[1].Value.ToString();
textBox3.Text = value2;
}
}
j += 2;
}
i++;
}
textBox3.Text = "无匹配";
}
...全文
634 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 wyd1520 的回复:
value1 = dataGridView1.Rows[0].Cells[16].Value.ToString(); DEBUG看看这个内容是什么。
这个值我debug了,可以出来,是一串数字,但是往下走就走不动了
本拉灯 2016-04-01
  • 打赏
  • 举报
回复
value1 = dataGridView1.Rows[0].Cells[16].Value.ToString(); DEBUG看看这个内容是什么。
全栈极简 2016-04-01
  • 打赏
  • 举报
回复
用int.tryparse方法强制转换,错误提示是值的格式不对,必须是数字。
江南小鱼 2016-04-01
  • 打赏
  • 举报
回复
这种问题debug一下很容易找到原因了
  • 打赏
  • 举报
回复
引用 2 楼 guwei4037 的回复:
用int.tryparse方法强制转换,错误提示是值的格式不对,必须是数字。
版主大人都出现了,太荣幸了,问题解决了 是我循环写错了、、、
  • 打赏
  • 举报
回复
引用 5 楼 szOOrz 的回复:

                        string num1 = (string)dataGridView2.Rows[i].Cells[j].Value.ToString();
                        int b=Int32.Parse(num1);
                        //int b = Convert.ToInt32(num1);
                        string num2 = (string)dataGridView2.Rows[i].Cells[j + 1].Value.ToString();
                        int c=Int32.Parse(num2);
                       // int c = Convert.ToInt32(num2);
改为:

                        string num1 = (string)dataGridView2.Rows[i].Cells[j].Value.ToString();
                        int b;
                        if(!int.TryParse(num1,out b))
                        {
                            MessageBox.Show(string.Format("'{0}' 无法转换为int类型",num1));
                            return;
                        }
                        string num2 = (string)dataGridView2.Rows[i].Cells[j + 1].Value.ToString();
                        int c;
                        if(!int.TryParse(num2 ,out c))
                        {
                            MessageBox.Show(string.Format("'{0}' 无法转换为int类型",num2));
                            return;
                        }
我debug了一下,发现是我的循环写错了,不过配上您这个语句更加严谨了,谢谢回答
qq_27202943 2016-04-01
  • 打赏
  • 举报
回复
.trim()
还在路上12138 2016-04-01
  • 打赏
  • 举报
回复
输入字符串的格式不正确 肯定是数据不是数字了,看看有没有空格或其他符号
好几只小萝莉 2016-04-01
  • 打赏
  • 举报
回复

                        string num1 = (string)dataGridView2.Rows[i].Cells[j].Value.ToString();
                        int b=Int32.Parse(num1);
                        //int b = Convert.ToInt32(num1);
                        string num2 = (string)dataGridView2.Rows[i].Cells[j + 1].Value.ToString();
                        int c=Int32.Parse(num2);
                       // int c = Convert.ToInt32(num2);
改为:

                        string num1 = (string)dataGridView2.Rows[i].Cells[j].Value.ToString();
                        int b;
                        if(!int.TryParse(num1,out b))
                        {
                            MessageBox.Show(string.Format("'{0}' 无法转换为int类型",num1));
                            return;
                        }
                        string num2 = (string)dataGridView2.Rows[i].Cells[j + 1].Value.ToString();
                        int c;
                        if(!int.TryParse(num2 ,out c))
                        {
                            MessageBox.Show(string.Format("'{0}' 无法转换为int类型",num2));
                            return;
                        }
tcf_2011 2016-04-01
  • 打赏
  • 举报
回复
是不是前后有空格之类的?

111,101

社区成员

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

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

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